Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow to create hydrator from concrete instance #2245

Merged
merged 1 commit into from

3 participants

@bakura10

This PR allows to create a form/fieldset using the factory and passing a concrete instance of a hydrator (furthermore, this is how it should work before, according to some docblocks in factory code).

@travisbot

This pull request passes (merged e31069c into 57c7d09).

@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2245] Updated README
- Indicated change in hydrator treatment by factory
2c12fd4
@weierophinney weierophinney merged commit e31069c into zendframework:master

1 check passed

Details default The Travis build passed
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2245] Updated README
- Indicated change in hydrator treatment by factory
fc9fb33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 26, 2012
  1. @bakura10
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 10 deletions.
  1. +16 −10 library/Zend/Form/Factory.php
  2. +12 −0 tests/ZendTest/Form/FactoryTest.php
View
26 library/Zend/Form/Factory.php
@@ -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
));
}
View
12 tests/ZendTest/Form/FactoryTest.php
@@ -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(
Something went wrong with that request. Please try again.