Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Hydrator aware interface #4170

Merged
merged 2 commits into from

3 participants

@ghunti

This is the same as #4016 but now only with the changes I've done.

@ghunti ghunti referenced this pull request
Closed

Hydrator aware interface #4016

@prolic

Seems like the tests for 5.3.3 are breaking.

@weierophinney weierophinney merged commit b08b5fe into zendframework:develop
@ghunti ghunti deleted the unknown repository branch
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/4170' into develop
Close #4170
c78521f
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-stdlib
@weierophinney weierophinney Merge pull request zendframework/zf2#4170 from ghunti/hydrator_aware_…
…interface_2

Hydrator aware interface
61de1af
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-stdlib
@weierophinney weierophinney Merge branch 'feature/4170' into develop 4295ab8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 2, 2013
  1. @ghunti
  2. @ghunti
This page is out of date. Refresh to see the latest.
View
10 library/Zend/Form/Fieldset.php
@@ -13,6 +13,7 @@
use Zend\Stdlib\Hydrator;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Stdlib\PriorityQueue;
+use Zend\Stdlib\Hydrator\HydratorAwareInterface;
class Fieldset extends Element implements FieldsetInterface
{
@@ -480,6 +481,9 @@ public function setHydrator(HydratorInterface $hydrator)
/**
* Get the hydrator used when binding an object to the fieldset
*
+ * If no hydrator is present and object implements HydratorAwareInterface,
+ * hydrator will be retrieved from the object.
+ *
* Will lazy-load Hydrator\ArraySerializable if none is present.
*
* @return HydratorInterface
@@ -487,7 +491,11 @@ public function setHydrator(HydratorInterface $hydrator)
public function getHydrator()
{
if (!$this->hydrator instanceof HydratorInterface) {
- $this->setHydrator(new Hydrator\ArraySerializable());
+ if ($this->object instanceof HydratorAwareInterface) {
+ $this->setHydrator($this->object->getHydrator());
+ } else {
+ $this->setHydrator(new Hydrator\ArraySerializable());
+ }
}
return $this->hydrator;
}
View
28 library/Zend/Stdlib/Hydrator/HydratorAwareInterface.php
@@ -0,0 +1,28 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace Zend\Stdlib\Hydrator;
+
+interface HydratorAwareInterface
+{
+ /**
+ * Set hydrator
+ *
+ * @param HydratorInterface $hydrator
+ * @return HydratorAwareInterface
+ */
+ public function setHydrator(HydratorInterface $hydrator);
+
+ /**
+ * Retrieve hydrator
+ *
+ * @return HydratorInterface
+ */
+ public function getHydrator();
+}
View
15 tests/ZendTest/Form/FormTest.php
@@ -20,6 +20,7 @@
use Zend\InputFilter\Factory as InputFilterFactory;
use Zend\Stdlib\Hydrator;
use ZendTest\Form\TestAsset\Entity;
+use ZendTest\Form\TestAsset\HydratorAwareModel;
class FormTest extends TestCase
{
@@ -621,6 +622,20 @@ public function testUsesBoundObjectAsDataSourceWhenNoDataSet()
$this->assertTrue($this->form->isValid());
}
+ public function testUsesBoundObjectHydratorToPopulateForm()
+ {
+ $this->populateForm();
+ $object = new HydratorAwareModel();
+ $object->setFoo('fooValue');
+ $object->setBar('barValue');
+
+ $this->form->bind($object);
+ $foo = $this->form->get('foo');
+ $this->assertEquals('fooValue', $foo->getValue());
+ $bar = $this->form->get('bar');
+ $this->assertEquals('barValue', $bar->getValue());
+ }
+
public function testBindOnValidateIsTrueByDefault()
{
$this->assertTrue($this->form->bindOnValidate());
View
58 tests/ZendTest/Form/TestAsset/HydratorAwareModel.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+*
+* @link http://github.com/zendframework/zf2 for the canonical source repository
+* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+* @license http://framework.zend.com/license/new-bsd New BSD License
+* @package Zend_Form
+*/
+
+namespace ZendTest\Form\TestAsset;
+
+use Zend\Stdlib\Hydrator\HydratorAwareInterface;
+use Zend\Stdlib\Hydrator\HydratorInterface;
+use Zend\Stdlib\Hydrator\ClassMethods;
+
+class HydratorAwareModel implements HydratorAwareInterface
+{
+ protected $hydrator = null;
+
+ protected $foo = null;
+ protected $bar = null;
+
+ public function setHydrator(HydratorInterface $hydrator)
+ {
+ $this->hydrator = $hydrator;
+ }
+
+ public function getHydrator()
+ {
+ if (!$this->hydrator) {
+ $this->hydrator = new ClassMethods();
+ }
+ return $this->hydrator;
+ }
+
+ public function setFoo($value)
+ {
+ $this->foo = $value;
+ return $this;
+ }
+
+ public function setBar($value)
+ {
+ $this->bar = $value;
+ return $this;
+ }
+
+ public function getFoo()
+ {
+ return $this->foo;
+ }
+
+ public function getBar()
+ {
+ return $this->bar;
+ }
+}
Something went wrong with that request. Please try again.