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

Commit

Permalink
Allow to create hydrator from concrete instance
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Aug 26, 2012
1 parent 57c7d09 commit e31069c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 16 additions & 10 deletions library/Zend/Form/Factory.php
Expand Up @@ -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
));
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ZendTest/Form/FactoryTest.php
Expand Up @@ -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(
Expand Down

0 comments on commit e31069c

Please sign in to comment.