Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added filter to arrayserializable

  • Loading branch information...
commit a534de4ef1ca1e34412118b5fb8eacbbb831136a 1 parent bfdfac3
@iwalz authored
View
10 library/Zend/Stdlib/Hydrator/AbstractHydrator.php
@@ -138,6 +138,16 @@ public function hydrateValue($name, $value)
}
/**
+ * Get the filter instance
+ *
+ * @return Filter\FilterComposite
+ */
+ public function getFilter()
+ {
+ return $this->filterComposite;
+ }
+
+ /**
* Add a new filter to take care of what needs to be hydrated.
* To exclude e.g. the method getServiceLocator:
*
View
9 library/Zend/Stdlib/Hydrator/ArraySerializable.php
@@ -39,9 +39,14 @@ public function extract($object)
$self = $this;
$data = $object->getArrayCopy();
- 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);
+ }
});
+ #var_dump($data);
return $data;
}
View
59 tests/ZendTest/Stdlib/HydratorTest.php
@@ -293,4 +293,63 @@ function($property) {
$datas = $hydrator->extract($this->classMethodsCamelCase);
$this->assertFalse(isset($datas['hasFoo']));
}
+
+ public function testArraySerializableFilter()
+ {
+ $hydrator = new \Zend\Stdlib\Hydrator\ArraySerializable();
+ $serializable = new \ZendTest\Stdlib\TestAsset\ArraySerializable();
+ $this->assertSame(
+ array(
+ "foo" => "bar",
+ "bar" => "foo",
+ "blubb" => "baz",
+ "quo" => "blubb"
+ ),
+ $hydrator->extract($serializable)
+ );
+
+ $hydrator->addFilter("foo", function($property) {
+ if ($property == "foo") {
+ return false;
+ }
+ return true;
+ });
+
+ $this->assertSame(
+ array(
+ "bar" => "foo",
+ "blubb" => "baz",
+ "quo" => "blubb"
+ ),
+ $hydrator->extract($serializable)
+ );
+
+ $hydrator->addFilter("len", function($property) {
+ if (strlen($property) !== 3) {
+ return false;
+ }
+ return true;
+ }, FilterComposite::CONDITION_AND);
+
+ $this->assertSame(
+ array(
+ "bar" => "foo",
+ "quo" => "blubb"
+ ),
+ $hydrator->extract($serializable)
+ );
+
+ $hydrator->removeFilter("len");
+ $hydrator->removeFilter("foo");
+
+ $this->assertSame(
+ array(
+ "foo" => "bar",
+ "bar" => "foo",
+ "blubb" => "baz",
+ "quo" => "blubb"
+ ),
+ $hydrator->extract($serializable)
+ );
+ }
}
View
54 tests/ZendTest/Stdlib/TestAsset/ArraySerializable.php
@@ -0,0 +1,54 @@
+<?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 ArraySerializable implements \Zend\Stdlib\ArraySerializableInterface
+{
+ protected $data = array();
+
+ public function __construct()
+ {
+ $this->data = array(
+ "foo" => "bar",
+ "bar" => "foo",
+ "blubb" => "baz",
+ "quo" => "blubb"
+ );
+ }
+
+ /**
+ * Exchange internal values from provided array
+ *
+ * @param array $array
+ * @return void
+ */
+ public function exchangeArray(array $array)
+ {
+ $this->data = $array;
+ }
+
+ /**
+ * Return an array representation of the object
+ *
+ * @return array
+ */
+ public function getArrayCopy()
+ {
+ return $this->data;
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.