2014-11-23 1 views
0

나는 Zend\Form\Element 인스턴스를 가지고 있으며이 인스턴스에서 array으로 모든 구성을 추출해야하며 &을 어딘가에 유지하고 나중에 양식 요소 팩토리에서 재사용하여 유사한 인스턴스를 다시 생성해야합니다.ZF2의 기존 요소 인스턴스에서 공장 친화적 인 폼 요소 구성 배열을 추출하는 방법은 무엇입니까?

zend framework 2에서 이미 인스턴스화 된 양식 요소 오브젝트에서 완전한 구성 서명을 얻는 프로그래밍 방식이 있습니까?

답변

2

짧은 대답은 아니오입니다. 너는 너의 것을 굴려야 할 것이다.

요소를 가져 와서 public 배열을 읽어 올바른 배열을 반환 할 수있는 독립적 인 클래스를 만들 수 있습니다. 이것은 실질적으로 FormFactory의 기능을 취소합니다.

아주 간단한 예를

class FormElementSerializer 
{ 
    public function toArray(ElementInterface $element) 
    { 
     $spec = $this->getElementSpec($element); 

     if ($element instanceof FieldsetInterface) { 
      $spec = $this->getFieldsetSpec($element, $spec); 
     } 

     if ($element instanceof Form) { 
      $spec = $this->getFormSpec($element, $spec); 
     } 

     return $spec; 
    } 

    protected function getElementSpec(ElementInterface $element) 
    { 
     $spec = array(
      'type' => $this->getElementType($element), 
      'name' => $element->getName(), 
      'options' => $element->getOptions(), 
      'attributes' => $element->getAttributes(), 
     ); 
     return $spec; 
    } 

    protected function getFieldsetSpec(FieldsetInterface $fieldset, array $spec) 
    { 
     foreach($fieldset->getElements() as $element) { 
      $spec['elements'][] = $this->getElementSpec($element); 
     } 
     return $spec; 
    } 

    // deals with hydrators, fieldsets etc 
    protected function getFormSpec(FormInterface $form, array $spec); 

    // could be as simple as returning the class name 
    protected function getElementType(ElementInterface $element); 
} 
관련 문제