Permalink
Browse files

Filter feature to object property hydrator

  • Loading branch information...
1 parent a534de4 commit 6df9a4f8317ab1b0f3c1307953a1c0ed499e28db @iwalz committed Jan 8, 2013
@@ -38,8 +38,12 @@ public function extract($object)
$self = $this;
$data = get_object_vars($object);
- array_walk($data, function (&$value, $name) use ($self) {
- $value = $self->extractValue($name, $value);
+ array_walk($data, function (&$value, $name) use ($self, &$data) {
+ if (!$self->getFilter()->filter($name)) {
+ unset($data[$name]);
+ } else {
+ $value = $self->extractValue($name, $value);
+ }
});
return $data;
}
@@ -12,14 +12,19 @@
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Stdlib\Hydrator\Reflection;
+use Zend\Stdlib\Hydrator\ObjectProperty;
+use Zend\Stdlib\Hydrator\ArraySerializable;
use Zend\Stdlib\Hydrator\Filter\FilterComposite;
use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase;
use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore;
use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing;
use ZendTest\Stdlib\TestAsset\Reflection as ReflectionAsset;
+use ZendTest\Stdlib\TestAsset\ObjectProperty as ObjectPropertyAsset;
+use ZendTest\Stdlib\TestAsset\ArraySerializable as ArraySerializableAsset;
use Zend\Stdlib\Hydrator\Strategy\DefaultStrategy;
use Zend\Stdlib\Hydrator\Strategy\SerializableStrategy;
+
/**
* @category Zend
* @package Zend_Stdlib
@@ -294,10 +299,11 @@ function($property) {
$this->assertFalse(isset($datas['hasFoo']));
}
- public function testArraySerializableFilter()
+ /**
+ * @dataProvider filterProvider
+ */
+ public function testArraySerializableFilter($hydrator, $serializable)
{
- $hydrator = new \Zend\Stdlib\Hydrator\ArraySerializable();
- $serializable = new \ZendTest\Stdlib\TestAsset\ArraySerializable();
$this->assertSame(
array(
"foo" => "bar",
@@ -352,4 +358,12 @@ public function testArraySerializableFilter()
$hydrator->extract($serializable)
);
}
+
+ public function filterProvider()
+ {
+ return array(
+ array(new ObjectProperty(), new ObjectPropertyAsset),
+ array(new ArraySerializable(), new ArraySerializableAsset)
+ );
+ }
}
@@ -0,0 +1,35 @@
+<?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
+ */
+
+namespace ZendTest\Stdlib\TestAsset;
+
+/**
+ * @category Zend
+ * @package Zend_Stdlib
+ * @subpackage UnitTests
+ * @group Zend_Stdlib
+ */
+class ObjectProperty
+{
+ public $foo = null;
+ public $bar = null;
+ public $blubb = null;
+ public $quo = null;
+
+ public function __construct()
+ {
+ $this->foo = "bar";
+ $this->bar = "foo";
+ $this->blubb = "baz";
+ $this->quo = "blubb";
+ }
+
+}
+

0 comments on commit 6df9a4f

Please sign in to comment.