Skip to content

Commit

Permalink
Merge branch 'hotfix/zendframework#7108-form-annotation-options-merge…
Browse files Browse the repository at this point in the history
…-support'

Close zendframework#7108
  • Loading branch information
Ocramius committed Jan 12, 2015
2 parents bf9649d + 6628ddf commit 5215ee4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
31 changes: 24 additions & 7 deletions library/Zend/Form/Annotation/ElementAnnotationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function handleComposedObjectAnnotation($e)

$elementSpec['spec']['type'] = 'Zend\Form\Element\Collection';
$elementSpec['spec']['name'] = $name;
$elementSpec['spec']['options'] = new ArrayObject($annotation->getOptions());
$elementSpec['spec']['options'] = new ArrayObject($this->mergeOptions($elementSpec, $annotation));
$elementSpec['spec']['options']['target_element'] = $specification;
$elementSpec['spec']['options']['target_element']['options']['input_filter_spec'] = $inputFilter;

Expand All @@ -163,14 +163,10 @@ public function handleComposedObjectAnnotation($e)
$specification['type'] = 'Zend\Form\Fieldset';
}

// Merge options of composed object with the ones of the target object:
$options = (isset($elementSpec['spec']['options']) && is_array($elementSpec['spec']['options'])) ? $elementSpec['spec']['options'] : array();
$options = array_merge($options, $annotation->getOptions());

// Add element spec:
$elementSpec['spec'] = $specification;
$elementSpec['spec']['name'] = $name;
$elementSpec['spec']['options'] = new ArrayObject($options);
$elementSpec['spec']['options'] = new ArrayObject($this->mergeOptions($elementSpec, $annotation));
}
}

Expand Down Expand Up @@ -324,7 +320,7 @@ public function handleOptionsAnnotation($e)
}

$elementSpec = $e->getParam('elementSpec');
$elementSpec['spec']['options'] = $annotation->getOptions();
$elementSpec['spec']['options'] = $this->mergeOptions($elementSpec, $annotation);
}

/**
Expand Down Expand Up @@ -396,4 +392,25 @@ public function handleValidatorAnnotation($e)
}
$inputSpec['validators'][] = $annotation->getValidator();
}

/**
* @param array $elementSpec
* @param ComposedObject|Options $annotation
*
* @return array
*/
private function mergeOptions(array $elementSpec, $annotation)
{
if (isset($elementSpec['spec']['options'])) {
if (is_array($elementSpec['spec']['options'])) {
return array_merge($elementSpec['spec']['options'], $annotation->getOptions());
}

if ($elementSpec['spec']['options'] instanceof ArrayObject) {
return array_merge($elementSpec['spec']['options']->getArrayCopy(), $annotation->getOptions());
}
}

return $annotation->getOptions();
}
}
29 changes: 29 additions & 0 deletions tests/ZendTest/Form/Annotation/AnnotationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,35 @@ public function testAllowsComposingMultipleChildEntities()
$this->assertTrue($target->has('password'));
}

/**
* @dataProvider provideOptionsAnnotationAndComposedObjectAnnotation
* @param string $childName
*
* @group 7108
*/
public function testOptionsAnnotationAndComposedObjectAnnotation($childName)
{
$entity = new TestAsset\Annotation\EntityUsingComposedObjectAndOptions();
$builder = new Annotation\AnnotationBuilder();
$form = $builder->createForm($entity);

$child = $form->get($childName);

$target = $child->getTargetElement();
$this->assertInstanceOf('Zend\Form\FieldsetInterface', $target);
$this->assertEquals('My label', $child->getLabel());
}

/**
* Data provider
*
* @return string[][]
*/
public function provideOptionsAnnotationAndComposedObjectAnnotation()
{
return array(array('child'), array('childTheSecond'));
}

public function testCanHandleOptionsAnnotation()
{
$entity = new TestAsset\Annotation\EntityUsingOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 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 EntityUsingComposedObjectAndOptions
{
/**
* @Annotation\Name("child")
* @Annotation\Options({"label": "My label"})
* @Annotation\ComposedObject({"target_object":"ZendTest\Form\TestAsset\Annotation\Entity", "is_collection":"true"})
*/
public $child;

/**
* @Annotation\Name("childTheSecond")
* @Annotation\ComposedObject({"target_object":"ZendTest\Form\TestAsset\Annotation\Entity", "is_collection":"true"})
* @Annotation\Options({"label": "My label"})
*/
public $childTheSecond;
}

0 comments on commit 5215ee4

Please sign in to comment.