Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Added ability to compose collections via Zend Form annotations #5420

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants
Contributor

carnage commented Nov 5, 2013

This patch resolves issue #5410

@weierophinney weierophinney commented on an outdated diff Nov 5, 2013

library/Zend/Form/Annotation/ComposedObject.php
+ *
+ * @return bool
+ */
+ public function isCollection()
+ {
+ return is_array($this->value) && isset($this->value['is_collection']) && $this->value['is_collection'];
+ }
+
+ /**
+ * Retrieve the options for the composed object
+ *
+ * @return array
+ */
+ public function getOptions()
+ {
+ return isset($this->value['options'])?$this->value['options']:array();
@weierophinney

weierophinney Nov 5, 2013

Owner

CS -- wrap the operators (? and :) with spaces.

@weierophinney weierophinney commented on the diff Nov 5, 2013

...y/Zend/Form/Annotation/ElementAnnotationsListener.php
@@ -136,8 +137,19 @@ public function handleComposedObjectAnnotation($e)
if (!isset($specification['type'])) {
$specification['type'] = 'Zend\Form\Fieldset';
}
- $elementSpec['spec'] = $specification;
- $elementSpec['spec']['name'] = $name;
+ if ($annotation->isCollection()) {
@weierophinney

weierophinney Nov 5, 2013

Owner

You need to check that the annotation is of the correct type before attempting to call isCollection on it.

Contributor

carnage commented Nov 6, 2013

It's not visible from the diff, but the method already checks that further up

 public function handleComposedObjectAnnotation($e)
    {
        $annotation = $e->getParam('annotation');
        if (!$annotation instanceof ComposedObject) {
            return;
        }

        $class             = $annotation->getComposedObject();
        $annotationManager = $e->getTarget();
        $specification     = $annotationManager->getFormSpecification($class);

        $name        = $e->getParam('name');
        $elementSpec = $e->getParam('elementSpec');
        $filterSpec  = $e->getParam('filterSpec');

        // Compose input filter into parent input filter
        $inputFilter = $specification['input_filter'];
        if (!isset($inputFilter['type'])) {
            $inputFilter['type'] = 'Zend\InputFilter\InputFilter';
        }
        $e->setParam('inputSpec', $inputFilter);
        unset($specification['input_filter']);

        // Compose specification as a fieldset into parent form/fieldset
        if (!isset($specification['type'])) {
            $specification['type'] = 'Zend\Form\Fieldset';
        }
        if ($annotation->isCollection()) {
            $elementSpec['spec']['type'] = 'Zend\Form\Element\Collection';
            $elementSpec['spec']['name'] = $name;
            $elementSpec['spec']['options'] = new ArrayObject($annotation->getOptions());
            $elementSpec['spec']['options']['target_element'] = $specification;

            if (isset($specification['hydrator'])) {
                $elementSpec['spec']['hydrator'] = $specification['hydrator'];
            }
        } else {
            $elementSpec['spec'] = $specification;
            $elementSpec['spec']['name'] = $name;
        }
    }

The whole method for reference

@weierophinney weierophinney added a commit that referenced this pull request Nov 6, 2013

@weierophinney weierophinney Merge pull request #5420 from carnage/master
Added ability to compose collections via Zend Form annotations
55dcf4a

@weierophinney weierophinney added a commit that referenced this pull request Nov 6, 2013

@weierophinney weierophinney Merge branch 'feature/5420' into develop
Close #5420
Fixes #5410
55298c6

@weierophinney weierophinney was assigned Nov 6, 2013

Owner

weierophinney commented Nov 6, 2013

Merged to develop for release with 2.3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment