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

Make allowObjectBinding configurable for Fieldsets #5741

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/Zend/Form/Annotation/ComposedObject.php
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
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
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
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);
}

}