Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Feature/context aware hydrator strategies #3580

Merged
merged 3 commits into from

6 participants

@stefankleff

Added ability to use the context in hydrator strategies.
This is very similar how validators are implemented.

...est/Stdlib/TestAsset/HydratorStrategyContextAware.php
@@ -0,0 +1,31 @@
+<?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_Stdlib
@Maks3w Collaborator
Maks3w added a note

Please remove this tag

The package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@EvanDotPro EvanDotPro was assigned
@ralphschindler
Collaborator

@EvanDotPro can you have a look at this?

@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#3580] Fix test failure
- $object was not being passed to closure that used it
61f084a
@weierophinney weierophinney merged commit 4632964 into from
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#3580] Fix test failure
- $object was not being passed to closure that used it
226a62c
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/3580' into develop
Close #3580
17c02af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 23, 2013
  1. @stefankleff
Commits on Jan 28, 2013
  1. @stefankleff

    Added ability to use the context in hydrator strategies.

    stefankleff authored
    This is very similar how validators are implemented.
Commits on Feb 1, 2013
  1. @stefankleff

    Remove package tag

    stefankleff authored
This page is out of date. Refresh to see the latest.
View
10 library/Zend/Stdlib/Hydrator/AbstractHydrator.php
@@ -104,13 +104,14 @@ public function removeStrategy($name)
*
* @param string $name The name of the strategy to use.
* @param mixed $value The value that should be converted.
+ * @param array $data The object is optionally provided as context.
* @return mixed
*/
- public function extractValue($name, $value)
+ public function extractValue($name, $value, $object = null)
{
if ($this->hasStrategy($name)) {
$strategy = $this->getStrategy($name);
- $value = $strategy->extract($value);
+ $value = $strategy->extract($value, $object);
}
return $value;
}
@@ -120,13 +121,14 @@ public function extractValue($name, $value)
*
* @param string $name The name of the strategy to use.
* @param mixed $value The value that should be converted.
+ * @param array $data The whole data is optionally provided as context.
* @return mixed
*/
- public function hydrateValue($name, $value)
+ public function hydrateValue($name, $value, $data = null)
{
if ($this->hasStrategy($name)) {
$strategy = $this->getStrategy($name);
- $value = $strategy->hydrate($value);
+ $value = $strategy->hydrate($value, $data);
}
return $value;
}
View
4 library/Zend/Stdlib/Hydrator/ClassMethods.php
@@ -142,7 +142,7 @@ public function extract($object)
if ($this->underscoreSeparatedKeys) {
$attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
}
- $attributes[$attribute] = $this->extractValue($attribute, $object->$method());
+ $attributes[$attribute] = $this->extractValue($attribute, $object->$method(), $object);
}
return $attributes;
@@ -178,7 +178,7 @@ public function hydrate(array $data, $object)
$method = preg_replace_callback('/(_[a-z])/', $transform, $method);
}
if (method_exists($object, $method)) {
- $value = $this->hydrateValue($property, $value);
+ $value = $this->hydrateValue($property, $value, $data);
$object->$method($value);
}
View
4 library/Zend/Stdlib/Hydrator/ObjectProperty.php
@@ -36,7 +36,7 @@ public function extract($object)
if (!$self->getFilter()->filter($name)) {
unset($data[$name]);
} else {
- $value = $self->extractValue($name, $value);
+ $value = $self->extractValue($name, $value, $object);
}
});
return $data;
@@ -60,7 +60,7 @@ public function hydrate(array $data, $object)
));
}
foreach ($data as $property => $value) {
- $object->$property = $this->hydrateValue($property, $value);
+ $object->$property = $this->hydrateValue($property, $value, $data);
}
return $object;
}
View
4 library/Zend/Stdlib/Hydrator/Reflection.php
@@ -36,7 +36,7 @@ public function extract($object)
}
$value = $property->getValue($object);
- $result[$propertyName] = $this->extractValue($propertyName, $value);
+ $result[$propertyName] = $this->extractValue($propertyName, $value, $object);
}
return $result;
@@ -54,7 +54,7 @@ public function hydrate(array $data, $object)
$reflProperties = self::getReflProperties($object);
foreach ($data as $key => $value) {
if (isset($reflProperties[$key])) {
- $reflProperties[$key]->setValue($object, $this->hydrateValue($key, $value));
+ $reflProperties[$key]->setValue($object, $this->hydrateValue($key, $value, $data));
}
}
return $object;
View
24 tests/ZendTest/Stdlib/HydratorStrategyTest.php
@@ -144,4 +144,28 @@ public function underscoreHandlingDataProvider()
array(false, 'fooBar'),
);
}
+
+ public function testContextAwarenessExtract()
+ {
+ $strategy = new TestAsset\HydratorStrategyContextAware();
+ $this->hydrator->addStrategy('field2', $strategy);
+
+ $entityB = new TestAsset\HydratorStrategyEntityB('X', 'Y');
+ $attributes = $this->hydrator->extract($entityB);
+
+ $this->assertEquals($entityB, $strategy->object);
+ }
+
+ public function testContextAwarenessHydrate()
+ {
+ $strategy = new TestAsset\HydratorStrategyContextAware();
+ $this->hydrator->addStrategy('field2', $strategy);
+
+ $entityB = new TestAsset\HydratorStrategyEntityB('X', 'Y');
+ $data = array('field1' => 'A', 'field2' => 'B');
+ $attributes = $this->hydrator->hydrate($data, $entityB);
+
+ $this->assertEquals($data, $strategy->data);
+ }
+
}
View
30 tests/ZendTest/Stdlib/TestAsset/HydratorStrategyContextAware.php
@@ -0,0 +1,30 @@
+<?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 ZendTest\Stdlib\TestAsset;
+
+use Zend\Stdlib\Hydrator\Strategy\DefaultStrategy;
+
+class HydratorStrategyContextAware extends DefaultStrategy
+{
+ public $object;
+ public $data;
+
+ public function extract($value, $object = null)
+ {
+ $this->object = $object;
+ return $value;
+ }
+
+ public function hydrate($value, $data = null)
+ {
+ $this->data = $data;
+ return $value;
+ }
+}
Something went wrong with that request. Please try again.