Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ability to compose collections via Zend Form annotations

  • Loading branch information...
commit 8c08cc5b0aaaf85b3c9e185562b0ec29c3a39ab0 1 parent 052e76f
@carnage authored
View
25 library/Zend/Form/Annotation/ComposedObject.php
@@ -19,7 +19,7 @@
*
* @Annotation
*/
-class ComposedObject extends AbstractStringAnnotation
+class ComposedObject extends AbstractArrayOrStringAnnotation
{
/**
* Retrieve the composed object classname
@@ -28,6 +28,29 @@ class ComposedObject extends AbstractStringAnnotation
*/
public function getComposedObject()
{
+ if (is_array($this->value)) {
+ return $this->value['target_object'];
+ }
return $this->value;
}
+
+ /**
+ * Is this composed object a collection or not
+ *
+ * @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();
+ }
}
View
16 library/Zend/Form/Annotation/ElementAnnotationsListener.php
@@ -10,6 +10,7 @@
namespace Zend\Form\Annotation;
use Zend\EventManager\EventManagerInterface;
+use Zend\Stdlib\ArrayObject;
/**
* Default listeners for element annotations
@@ -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()) {
+ $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;
+ }
}
/**
View
16 tests/ZendTest/Form/Annotation/AnnotationBuilderTest.php
@@ -168,6 +168,22 @@ public function testAllowsComposingChildEntities()
$this->assertTrue($composed->has('password'));
}
+ public function testAllowsComposingMultipleChildEntities()
+ {
+ $entity = new TestAsset\Annotation\EntityComposingMultipleEntities();
+ $builder = new Annotation\AnnotationBuilder();
+ $form = $builder->createForm($entity);
+
+ $this->assertTrue($form->has('composed'));
+ $composed = $form->get('composed');
+
+ $this->assertInstanceOf('Zend\Form\Element\Collection', $composed);
+ $target = $composed->getTargetElement();
+ $this->assertInstanceOf('Zend\Form\FieldsetInterface', $target);
+ $this->assertTrue($target->has('username'));
+ $this->assertTrue($target->has('password'));
+ }
+
public function testCanHandleOptionsAnnotation()
{
$entity = new TestAsset\Annotation\EntityUsingOptions();
View
24 tests/ZendTest/Form/TestAsset/Annotation/EntityComposingMultipleEntities.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendTest\Form\TestAsset\Annotation;
+
+use Zend\Form\Annotation;
+
+/**
+ * @Annotation\Name("hierarchical")
+ */
+class EntityComposingMultipleEntities
+{
+ /**
+ * @Annotation\Name("composed")
+ * @Annotation\ComposedObject({"target_object":"ZendTest\Form\TestAsset\Annotation\Entity", "is_collection":"true"})
+ */
+ public $child;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.