Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow to create hydrator from concrete instance

  • Loading branch information...
commit e31069c3e232311d674436c105be5b95e9d53d71 1 parent 57c7d09
Michaël Gallego bakura10 authored
Showing with 28 additions and 10 deletions.
  1. +16 −10 library/Zend/Form/Factory.php
  2. +12 −0 tests/ZendTest/Form/FactoryTest.php
26 library/Zend/Form/Factory.php
View
@@ -348,39 +348,45 @@ protected function prepareAndInjectObject($objectName, FieldsetInterface $fields
/**
* Prepare and inject a named hydrator
*
- * Takes a string indicating a hydrator class name, instantiates the class
+ * Takes a string indicating a hydrator class name (or a concrete instance), instantiates the class
* by that name, and injects the hydrator instance into the form.
*
- * @param string $hydratorName
+ * @param string $hydratorOrName
* @param FieldsetInterface $fieldset
* @param string $method
* @return void
- * @throws Exception\DomainException if $hydratorName is not a string, does not resolve to a known class, or the class does not implement Hydrator\HydratorInterface
+ * @throws Exception\DomainException If $hydratorOrName is not a string, does not resolve to a known class, or
+ * the class does not implement Hydrator\HydratorInterface
*/
- protected function prepareAndInjectHydrator($hydratorName, FieldsetInterface $fieldset, $method)
+ protected function prepareAndInjectHydrator($hydratorOrName, FieldsetInterface $fieldset, $method)
{
- if (!is_string($hydratorName)) {
+ if (is_object($hydratorOrName) && $hydratorOrName instanceof Hydrator\HydratorInterface) {
+ $fieldset->setHydrator($hydratorOrName);
+ return;
+ }
+
+ if (!is_string($hydratorOrName)) {
throw new Exception\DomainException(sprintf(
'%s expects string hydrator class name; received "%s"',
$method,
- (is_object($hydratorName) ? get_class($hydratorName) : gettype($hydratorName))
+ (is_object($hydratorOrName) ? get_class($hydratorOrName) : gettype($hydratorOrName))
));
}
- if (!class_exists($hydratorName)) {
+ if (!class_exists($hydratorOrName)) {
throw new Exception\DomainException(sprintf(
'%s expects string hydrator name to be a valid class name; received "%s"',
$method,
- $hydratorName
+ $hydratorOrName
));
}
- $hydrator = new $hydratorName;
+ $hydrator = new $hydratorOrName;
if (!$hydrator instanceof Hydrator\HydratorInterface) {
throw new Exception\DomainException(sprintf(
'%s expects a valid implementation of Zend\Form\Hydrator\HydratorInterface; received "%s"',
$method,
- $hydratorName
+ $hydratorOrName
));
}
12 tests/ZendTest/Form/FactoryTest.php
View
@@ -317,6 +317,18 @@ public function testCanCreateFormsAndSpecifyHydrator()
$this->assertInstanceOf('Zend\Stdlib\Hydrator\ObjectProperty', $hydrator);
}
+ public function testCanCreateHydratorFromConcreteClass()
+ {
+ $form = $this->factory->createForm(array(
+ 'name' => 'foo',
+ 'hydrator' => new \Zend\Stdlib\Hydrator\ObjectProperty()
+ ));
+
+ $this->assertInstanceOf('Zend\Form\FormInterface', $form);
+ $hydrator = $form->getHydrator();
+ $this->assertInstanceOf('Zend\Stdlib\Hydrator\ObjectProperty', $hydrator);
+ }
+
public function testCanCreateFormWithHydratorAndInputFilterAndElementsAndFieldsets()
{
$form = $this->factory->createForm(array(
Please sign in to comment.
Something went wrong with that request. Please try again.