Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5741' into develop
Browse files Browse the repository at this point in the history
Close #5741
  • Loading branch information
weierophinney committed Mar 7, 2014
2 parents 9969cb4 + d79aa25 commit 667ea76
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion library/Zend/Form/Annotation/ComposedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public function isCollection()
*/
public function getOptions()
{
return isset($this->value['options']) ? $this->value['options'] : array();
return is_array($this->value) && isset($this->value['options']) ? $this->value['options'] : array();
}
}
6 changes: 6 additions & 0 deletions library/Zend/Form/Annotation/ElementAnnotationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,14 @@ 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);
}
}

Expand Down
44 changes: 43 additions & 1 deletion library/Zend/Form/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Form;

use Traversable;
use Zend\Code\Reflection\ClassReflection;
use Zend\Stdlib\Hydrator;
use Zend\Stdlib\Hydrator\HydratorAwareInterface;
use Zend\Stdlib\Hydrator\HydratorInterface;
Expand Down Expand Up @@ -68,6 +69,13 @@ class Fieldset extends Element implements FieldsetInterface
*/
protected $useAsBaseFieldset = false;

/**
* The class or interface of objects that can be bound to this fieldset.
*
* @var string
*/
protected $allowedObjectBindingClass;

/**
* @param null|int|string $name Optional name for the element
* @param array $options Optional options for the element
Expand All @@ -94,6 +102,10 @@ public function setOptions($options)
$this->setUseAsBaseFieldset($options['use_as_base_fieldset']);
}

if (isset($options['allowed_object_binding_class'])) {
$this->setAllowedObjectBindingClass($options['allowed_object_binding_class']);
}

return $this;
}

Expand Down Expand Up @@ -465,6 +477,26 @@ public function getObject()
return $this->object;
}

/**
* Set the class or interface of objects that can be bound to this fieldset.
*
* @param string $allowObjectBindingClass
*/
public function setAllowedObjectBindingClass($allowObjectBindingClass)
{
$this->allowedObjectBindingClass = $allowObjectBindingClass;
}

/**
* Get The class or interface of objects that can be bound to this fieldset.
*
* @return string
*/
public function allowedObjectBindingClass()
{
return $this->allowedObjectBindingClass;
}

/**
* Checks if the object can be set in this fieldset
*
Expand All @@ -473,7 +505,17 @@ public function getObject()
*/
public function allowObjectBinding($object)
{
return ($this->object && $object instanceof $this->object);
$validBindingClass = false;
if (is_object($object) && $this->allowedObjectBindingClass()) {
$objectClass = ltrim($this->allowedObjectBindingClass(), '\\');
$reflection = new ClassReflection($object);
$validBindingClass = (
$reflection->getName() == $objectClass
|| $reflection->isSubclassOf($this->allowedObjectBindingClass())
);
}

return ($validBindingClass || $this->object && $object instanceof $this->object);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/ZendTest/Form/FieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,16 @@ public function testSetOptionsUseAsBaseFieldset()
$this->assertEquals('bar', $option);
}

public function testSetOptionAllowedObjectBindingClass()
{
$this->fieldset->setOptions(array(
'allowed_object_binding_class' => 'bar'
));
$option = $this->fieldset->getOption('allowed_object_binding_class');

$this->assertEquals('bar', $option);
}

/**
* @expectedException Zend\Form\Exception\InvalidElementException
*/
Expand All @@ -471,4 +481,23 @@ public function testSetObjectWithStringRaisesException()
$this->setExpectedException('Zend\Form\Exception\InvalidArgumentException');
$this->fieldset->setObject('foo');
}

public function testShouldValidateAllowObjectBindingByClassname()
{
$object = new \stdClass();
$this->fieldset->setAllowedObjectBindingClass('stdClass');
$allowed = $this->fieldset->allowObjectBinding($object);

$this->assertTrue($allowed);
}

public function testShouldValidateAllowObjectBindingByObject()
{
$object = new \stdClass();
$this->fieldset->setObject($object);
$allowed = $this->fieldset->allowObjectBinding($object);

$this->assertTrue($allowed);
}

}

0 comments on commit 667ea76

Please sign in to comment.