From 874aec176dc49e288664fd4d578d6a371ef53d2a Mon Sep 17 00:00:00 2001 From: ingo Date: Mon, 7 Jan 2013 00:59:43 +0100 Subject: [PATCH 01/13] Unit tests and error handling --- src/Hydrator/ClassMethods.php | 2 - src/Hydrator/Filter/FilterComposite.php | 41 ++++++++++- test/TestAsset/FilterCompositeTest.php | 90 +++++++++++++++++++------ 3 files changed, 110 insertions(+), 23 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 003424512..bff9f3fe7 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -78,7 +78,6 @@ public function setOptions($options) public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys) { $this->underscoreSeparatedKeys = $underscoreSeparatedKeys; - return $this; } @@ -88,7 +87,6 @@ public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys) public function getUnderscoreSeparatedKeys() { return $this->underscoreSeparatedKeys; - } /** diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index 87f3efcf1..e21e8af06 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -9,6 +9,8 @@ */ namespace Zend\Stdlib\Hydrator\Filter; +use Zend\Stdlib\Exception\InvalidArgumentException; + /** * @category Zend * @package Zend_Stdlib @@ -36,9 +38,39 @@ class FilterComposite implements FilterInterface /** * Define default Filter + * + * @throws InvalidArgumentException */ public function __construct($orFilter = array(), $andFilter = array()) { + array_walk($orFilter, + function($value, $key) { + if( + !is_callable($value) && + !$value instanceof FilterInterface + ) { + throw new InvalidArgumentException( + 'The value of ' . $key . ' should be either a callable or ' . + 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' + ); + } + } + ); + + array_walk($andFilter, + function($value, $key) { + if( + !is_callable($value) && + !$value instanceof FilterInterface + ) { + throw new InvalidArgumentException( + 'The value of ' . $key . ' should be either a callable or ' . + 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' + ); + } + } + ); + $this->orFilter = new \ArrayObject($orFilter); $this->andFilter = new \ArrayObject($andFilter); } @@ -62,6 +94,7 @@ public function __construct($orFilter = array(), $andFilter = array()) * @param string $name * @param callable|FilterInterface $filter * @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND + * @throws InvalidArgumentException */ public function addFilter($name, $filter, $condition = self::CONDITION_OR) { @@ -71,10 +104,14 @@ public function addFilter($name, $filter, $condition = self::CONDITION_OR) ) { if ($condition === self::CONDITION_OR) { $this->orFilter[$name] = $filter; - } - if ($condition === self::CONDITION_AND) { + } elseif ($condition === self::CONDITION_AND) { $this->andFilter[$name] = $filter; } + } else { + throw new InvalidArgumentException( + 'The value of ' . $name . ' should be either a callable or ' . + 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' + ); } } diff --git a/test/TestAsset/FilterCompositeTest.php b/test/TestAsset/FilterCompositeTest.php index 9eaa21e5e..6efbbfaa7 100644 --- a/test/TestAsset/FilterCompositeTest.php +++ b/test/TestAsset/FilterCompositeTest.php @@ -12,56 +12,56 @@ use Zend\Stdlib\Hydrator\Filter\FilterComposite; -class ValidationCompositeTest extends \PHPUnit_Framework_TestCase +class FilterCompositeTest extends \PHPUnit_Framework_TestCase { - protected $validatorComposite; + protected $filterComposite; public function setUp() { - $this->validatorComposite = new FilterComposite(); + $this->filterComposite = new FilterComposite(); } public function testValidationAdd() { - $this->assertTrue($this->validatorComposite->filter("foo")); - $this->validatorComposite->addFilter("has", + $this->assertTrue($this->filterComposite->filter("foo")); + $this->filterComposite->addFilter("has", function($property) { return false; } ); - $this->assertFalse($this->validatorComposite->filter("foo")); + $this->assertFalse($this->filterComposite->filter("foo")); } public function testValidationRemove() { - $this->validatorComposite->addFilter("has", + $this->filterComposite->addFilter("has", function($property) { return false; } ); - $this->assertFalse($this->validatorComposite->filter("foo")); - $this->validatorComposite->removeFilter("has"); - $this->assertTrue($this->validatorComposite->filter("foo")); + $this->assertFalse($this->filterComposite->filter("foo")); + $this->filterComposite->removeFilter("has"); + $this->assertTrue($this->filterComposite->filter("foo")); } public function testValidationHas() { - $this->validatorComposite->addFilter("has", + $this->filterComposite->addFilter("has", function($property) { return false; } ); - $this->assertFalse($this->validatorComposite->filter("foo")); - $this->assertTrue($this->validatorComposite->hasFilter("has")); + $this->assertFalse($this->filterComposite->filter("foo")); + $this->assertTrue($this->filterComposite->hasFilter("has")); } public function testComplexValidation() { - $this->validatorComposite->addFilter("has", new \Zend\Stdlib\Hydrator\Filter\HasFilter()); - $this->validatorComposite->addFilter("get", new \Zend\Stdlib\Hydrator\Filter\GetFilter()); - $this->validatorComposite->addFilter("is", new \Zend\Stdlib\Hydrator\Filter\IsFilter()); + $this->filterComposite->addFilter("has", new \Zend\Stdlib\Hydrator\Filter\HasFilter()); + $this->filterComposite->addFilter("get", new \Zend\Stdlib\Hydrator\Filter\GetFilter()); + $this->filterComposite->addFilter("is", new \Zend\Stdlib\Hydrator\Filter\IsFilter()); - $this->validatorComposite->addFilter("exclude", + $this->filterComposite->addFilter("exclude", function($property) { $method = substr($property, strpos($property, '::')); @@ -73,7 +73,59 @@ function($property) { }, FilterComposite::CONDITION_AND ); - $this->assertTrue($this->validatorComposite->filter('getFooBar')); - $this->assertFalse($this->validatorComposite->filter('getServiceLocator')); + $this->assertTrue($this->filterComposite->filter('getFooBar')); + $this->assertFalse($this->filterComposite->filter('getServiceLocator')); + } + + public function testConstructorInjection() + { + $andCondition = array( + 'servicelocator' => function($property) { + if($property === 'getServiceLocator') { + return false; + } + return true; + }, + 'foobar' => function($property) { + if($property === 'getFooBar') { + return false; + } + return true; + } + ); + $orCondition = array( + 'has' => new \Zend\Stdlib\Hydrator\Filter\HasFilter(), + 'get' => new \Zend\Stdlib\Hydrator\Filter\GetFilter() + ); + $filterComposite = new FilterComposite($orCondition, $andCondition); + + $this->assertFalse($filterComposite->filter('getFooBar')); + $this->assertFalse($filterComposite->filter('geTFooBar')); + $this->assertFalse($filterComposite->filter('getServiceLocator')); + $this->assertTrue($filterComposite->filter('getFoo')); + $this->assertTrue($filterComposite->filter('hasFoo')); + } + + /** + * @expectedException Zend\Stdlib\Exception\InvalidArgumentException + * @expectedExceptionMessage The value of test should be either a callable + * or an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface + */ + public function testInvalidParameterConstructorInjection() + { + $andCondition = array('foo' => 'bar'); + $orCondition = array('test' => 'blubb'); + + new FilterComposite($orCondition, $andCondition); + } + + /** + * @expectedException Zend\Stdlib\Exception\InvalidArgumentException + * @expectedExceptionMessage The value of foo should be either a callable + * or an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface + */ + public function testInvalidFilterInjection() + { + $this->filterComposite->addFilter('foo', 'bar'); } } From 85dd79a63ff5bf36a3400965c390104e2b9c0cd9 Mon Sep 17 00:00:00 2001 From: ingo Date: Tue, 8 Jan 2013 00:38:56 +0100 Subject: [PATCH 02/13] Fixed reviews --- src/Hydrator/ClassMethods.php | 10 +++--- src/Hydrator/Filter/FilterComposite.php | 47 ++++++++++++++----------- test/HydratorTest.php | 2 +- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index bff9f3fe7..54d3be736 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -10,11 +10,11 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Exception, - Zend\Stdlib\Hydrator\Filter\FilterComposite, - Zend\Stdlib\Hydrator\Filter\IsFilter, - Zend\Stdlib\Hydrator\Filter\GetFilter, - Zend\Stdlib\Hydrator\Filter\HasFilter; +use Zend\Stdlib\Exception; +use Zend\Stdlib\Hydrator\Filter\FilterComposite; +use Zend\Stdlib\Hydrator\Filter\GetFilter; +use Zend\Stdlib\Hydrator\Filter\HasFilter; +use Zend\Stdlib\Hydrator\Filter\IsFilter; /** * @category Zend diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index e21e8af06..03767936b 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -9,6 +9,7 @@ */ namespace Zend\Stdlib\Hydrator\Filter; +use ArrayObject; use Zend\Stdlib\Exception\InvalidArgumentException; /** @@ -19,11 +20,12 @@ class FilterComposite implements FilterInterface { /** - * @var \ArrayObject + * @var ArrayObject */ protected $orFilter; + /** - * @var \ArrayObject + * @var ArrayObject */ protected $andFilter; @@ -31,6 +33,7 @@ class FilterComposite implements FilterInterface * Constant to add with "or" conditition */ const CONDITION_OR = 1; + /** * Constant to add with "and" conditition */ @@ -46,8 +49,8 @@ public function __construct($orFilter = array(), $andFilter = array()) array_walk($orFilter, function($value, $key) { if( - !is_callable($value) && - !$value instanceof FilterInterface + !is_callable($value) + && !$value instanceof FilterInterface ) { throw new InvalidArgumentException( 'The value of ' . $key . ' should be either a callable or ' . @@ -60,8 +63,8 @@ function($value, $key) { array_walk($andFilter, function($value, $key) { if( - !is_callable($value) && - !$value instanceof FilterInterface + !is_callable($value) + && !$value instanceof FilterInterface ) { throw new InvalidArgumentException( 'The value of ' . $key . ' should be either a callable or ' . @@ -71,8 +74,8 @@ function($value, $key) { } ); - $this->orFilter = new \ArrayObject($orFilter); - $this->andFilter = new \ArrayObject($andFilter); + $this->orFilter = new ArrayObject($orFilter); + $this->andFilter = new ArrayObject($andFilter); } /** @@ -95,30 +98,31 @@ function($value, $key) { * @param callable|FilterInterface $filter * @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND * @throws InvalidArgumentException + * @return FilterComposite */ public function addFilter($name, $filter, $condition = self::CONDITION_OR) { - if ( - is_callable($filter) || - $filter instanceof FilterInterface - ) { - if ($condition === self::CONDITION_OR) { - $this->orFilter[$name] = $filter; - } elseif ($condition === self::CONDITION_AND) { - $this->andFilter[$name] = $filter; - } - } else { + if ( !is_callable($filter) && !($filter instanceof FilterInterface) ) { throw new InvalidArgumentException( 'The value of ' . $name . ' should be either a callable or ' . 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface' ); } + + if ($condition === self::CONDITION_OR) { + $this->orFilter[$name] = $filter; + } elseif ($condition === self::CONDITION_AND) { + $this->andFilter[$name] = $filter; + } + + return $this; } /** * Remove a filter from the composition * * @param $name string Identifier for the filter + * @return FilterComposite */ public function removeFilter($name) { @@ -129,6 +133,8 @@ public function removeFilter($name) if( isset($this->andFilter[$name])) { unset($this->andFilter[$name]); } + + return $this; } /** @@ -139,8 +145,7 @@ public function removeFilter($name) */ public function hasFilter($name) { - return - isset($this->orFilter[$name]) || isset($this->andFilter[$name]); + return isset($this->orFilter[$name]) || isset($this->andFilter[$name]); } /** @@ -170,6 +175,7 @@ public function filter($property) $returnValue = true; break; } + continue; } else { if ( $filter->filter($property) === true) { $returnValue = true; @@ -184,6 +190,7 @@ public function filter($property) if( $filter($property) === false) { return false; } + continue; } else { if( $filter->filter($property) === false) { return false; diff --git a/test/HydratorTest.php b/test/HydratorTest.php index d16193ef0..4f178daf2 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -274,7 +274,7 @@ public function testHydratorClassMethodsManipulateFilter() $this->assertFalse(isset($datas['hasBar'])); //method is hasBar } - public function testHydratorClassMethodsWithCostumFilter() + public function testHydratorClassMethodsWithCustomFilter() { $hydrator = new ClassMethods(false); $datas = $hydrator->extract($this->classMethodsCamelCase); From 02528a748582445a594aaac0fc5ae3fcc40b0d65 Mon Sep 17 00:00:00 2001 From: ingo Date: Tue, 8 Jan 2013 01:11:17 +0100 Subject: [PATCH 03/13] Moved filter implementation to abstract hydrator --- src/Hydrator/AbstractHydrator.php | 61 +++++++++++++++++++++++++++++++ src/Hydrator/ClassMethods.php | 58 ----------------------------- 2 files changed, 61 insertions(+), 58 deletions(-) diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 056ade4b0..5d5dd56a4 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -12,6 +12,7 @@ use ArrayObject; use Zend\Stdlib\Exception; +use Zend\Stdlib\Hydrator\Filter\FilterComposite; use Zend\Stdlib\Hydrator\StrategyEnabledInterface; use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; @@ -29,12 +30,19 @@ abstract class AbstractHydrator implements HydratorInterface, StrategyEnabledInt */ protected $strategies; + /** + * Composite to filter the methods, that need to be hydrated + * @var Filter\FilterComposite + */ + protected $filterComposite; + /** * Initializes a new instance of this class. */ public function __construct() { $this->strategies = new ArrayObject(); + $this->filterComposite = new FilterComposite(); } /** @@ -128,4 +136,57 @@ public function hydrateValue($name, $value) } return $value; } + + /** + * Add a new filter to take care of what needs to be hydrated. + * To exclude e.g. the method getServiceLocator: + * + * + * $composite->addFilter("servicelocator", + * function($property) { + * list($class, $method) = explode('::', $property); + * if ($method === 'getServiceLocator') { + * return false; + * } + * return true; + * }, FilterComposite::CONDITION_AND + * ); + * + * + * @param string $name Index in the composite + * @param callable|Zend\Stdlib\Hydrator\Filter\FilterInterface $filter + * @param int $condition + * @return Filter\FilterComposite + */ + public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR) + { + return $this->filterComposite->addFilter($name, $filter, $condition); + } + + /** + * Check whether a specific filter exists at key $name or not + * + * @param string $name Index in the composite + * @return bool + */ + public function hasFilter($name) + { + return $this->filterComposite->hasFilter($name); + } + + /** + * Remove a filter from the composition. + * To not extract "has" methods, you simply need to unregister it + * + * + * $filterComposite->removeFilter('has'); + * + * + * @param $name + * @return Filter\FilterComposite + */ + public function removeFilter($name) + { + return $this->filterComposite->removeFilter($name); + } } diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 54d3be736..4cb37c4e4 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -11,7 +11,6 @@ namespace Zend\Stdlib\Hydrator; use Zend\Stdlib\Exception; -use Zend\Stdlib\Hydrator\Filter\FilterComposite; use Zend\Stdlib\Hydrator\Filter\GetFilter; use Zend\Stdlib\Hydrator\Filter\HasFilter; use Zend\Stdlib\Hydrator\Filter\IsFilter; @@ -29,12 +28,6 @@ class ClassMethods extends AbstractHydrator implements HydratorOptionsInterface */ protected $underscoreSeparatedKeys = true; - /** - * Composite to filter the methods, that need to be hydrated - * @var Filter\FilterComposite - */ - protected $filterComposite; - /** * Define if extract values will use camel case or name with underscore * @param boolean|array $underscoreSeparatedKeys @@ -44,7 +37,6 @@ public function __construct($underscoreSeparatedKeys = true) parent::__construct(); $this->setUnderscoreSeparatedKeys($underscoreSeparatedKeys); - $this->filterComposite = new FilterComposite(); $this->filterComposite->addFilter("is", new IsFilter()); $this->filterComposite->addFilter("has", new HasFilter()); $this->filterComposite->addFilter("get", new GetFilter()); @@ -174,54 +166,4 @@ public function hydrate(array $data, $object) return $object; } - /** - * Add a new filter to take care of what needs to be hydrated. - * To exclude e.g. the method getServiceLocator: - * - * - * $composite->addFilter("servicelocator", - * function($property) { - * list($class, $method) = explode('::', $property); - * if ($method === 'getServiceLocator') { - * return false; - * } - * return true; - * }, FilterComposite::CONDITION_AND - * ); - * - * - * @param string $name Index in the composite - * @param callable|Zend\Stdlib\Hydrator\Filter\FilterInterface $filter - * @param int $condition - */ - public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR) - { - $this->filterComposite->addFilter($name, $filter, $condition); - } - - /** - * Check whether a specific filter exists at key $name or not - * - * @param string $name Index in the composite - * @return bool - */ - public function hasFilter($name) - { - return $this->filterComposite->hasFilter($name); - } - - /** - * Remove a filter from the composition. - * To not extract "has" methods, you simply need to unregister it - * - * - * $filterComposite->removeFilter('has'); - * - * - * @param $name - */ - public function removeFilter($name) - { - $this->filterComposite->removeFilter($name); - } } From 09d682ec4c2d3c88a706cf96463ae3697d9dc3a3 Mon Sep 17 00:00:00 2001 From: ingo Date: Tue, 8 Jan 2013 22:09:42 +0100 Subject: [PATCH 04/13] Fixed complex composite --- src/Hydrator/Filter/FilterComposite.php | 11 ++++-- test/TestAsset/FilterCompositeTest.php | 49 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index 03767936b..00a99477a 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -158,16 +158,21 @@ public function hasFilter($name) */ public function filter($property) { + $andCount = count($this->andFilter); + $orCount = count($this->orFilter); // return true if no filters are registered if ( - count($this->orFilter) === 0 && - count($this->andFilter) === 0 + $orCount === 0 && + $andCount === 0 ) { return true; + } elseif ($orCount === 0 && $andCount !== 0) { + $returnValue = true; + } else { + $returnValue = false; } // Check if 1 from the or filters return true - $returnValue = false; foreach($this->orFilter as $filter) { if (is_callable($filter)) { if( $filter($property) === true) diff --git a/test/TestAsset/FilterCompositeTest.php b/test/TestAsset/FilterCompositeTest.php index 6efbbfaa7..cf278afac 100644 --- a/test/TestAsset/FilterCompositeTest.php +++ b/test/TestAsset/FilterCompositeTest.php @@ -106,6 +106,55 @@ public function testConstructorInjection() $this->assertTrue($filterComposite->filter('hasFoo')); } + public function testWithOnlyAndFiltersAdded() + { + $filter = new FilterComposite(); + $filter->addFilter("foobarbaz", function($property) { + return true; + }, FilterComposite::CONDITION_AND); + $filter->addFilter("foobar", function($property) { + return true; + }, FilterComposite::CONDITION_AND); + $this->assertTrue($filter->filter("foo")); + } + + public function testWithOnlyOrFiltersAdded() + { + $filter = new FilterComposite(); + $filter->addFilter("foobarbaz", function($property) { + return true; + }); + $filter->addFilter("foobar", function($property) { + return false; + }); + $this->assertTrue($filter->filter("foo")); + } + + public function testWithComplexCompositeAdded() + { + $filter1 = new FilterComposite(); + $filter1->addFilter("foobarbaz", function($property) { + return true; + }); + $filter1->addFilter("foobar", function($property) { + return false; + }); + $filter2 = new FilterComposite(); + $filter2->addFilter("bar", function($property) { + return true; + }, FilterComposite::CONDITION_AND); + $filter2->addFilter("barblubb", function($property) { + return true; + }, FilterComposite::CONDITION_AND); + $this->assertTrue($filter1->filter("foo")); + $this->assertTrue($filter2->filter("foo")); + $filter1->addFilter("bar", $filter2); + $this->assertTrue($filter1->filter("blubb")); + + $filter1->addFilter("blubb", function($property) { return false; }, FilterComposite::CONDITION_AND); + $this->assertFalse($filter1->filter("test")); + } + /** * @expectedException Zend\Stdlib\Exception\InvalidArgumentException * @expectedExceptionMessage The value of test should be either a callable From d23d89d0cbeb2b122e88f765c6790bd041773605 Mon Sep 17 00:00:00 2001 From: ingo Date: Tue, 8 Jan 2013 23:37:41 +0100 Subject: [PATCH 05/13] Added filter to arrayserializable --- src/Hydrator/AbstractHydrator.php | 10 +++++ src/Hydrator/ArraySerializable.php | 9 ++++- test/HydratorTest.php | 59 ++++++++++++++++++++++++++++ test/TestAsset/ArraySerializable.php | 54 +++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 test/TestAsset/ArraySerializable.php diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 5d5dd56a4..19ac954d7 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -137,6 +137,16 @@ public function hydrateValue($name, $value) return $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: diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index fbe214db9..da16c4981 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/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; } diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 4f178daf2..44dc16f3a 100644 --- a/test/HydratorTest.php +++ b/test/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) + ); + } } diff --git a/test/TestAsset/ArraySerializable.php b/test/TestAsset/ArraySerializable.php new file mode 100644 index 000000000..498099c5f --- /dev/null +++ b/test/TestAsset/ArraySerializable.php @@ -0,0 +1,54 @@ +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; + } +} + From d7c2600e4a5325687463998af21465b5080f1250 Mon Sep 17 00:00:00 2001 From: ingo Date: Tue, 8 Jan 2013 23:54:00 +0100 Subject: [PATCH 06/13] Filter feature to object property hydrator --- src/Hydrator/ObjectProperty.php | 8 +++++-- test/HydratorTest.php | 20 +++++++++++++++--- test/TestAsset/ObjectProperty.php | 35 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 test/TestAsset/ObjectProperty.php diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index a77e1e792..dd5f1a0dd 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -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; } diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 44dc16f3a..f51b09355 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -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) + ); + } } diff --git a/test/TestAsset/ObjectProperty.php b/test/TestAsset/ObjectProperty.php new file mode 100644 index 000000000..6b73900ee --- /dev/null +++ b/test/TestAsset/ObjectProperty.php @@ -0,0 +1,35 @@ +foo = "bar"; + $this->bar = "foo"; + $this->blubb = "baz"; + $this->quo = "blubb"; + } + +} + From 7be4274ae18df4782c40a104aeb62c5655fcb819 Mon Sep 17 00:00:00 2001 From: ingo Date: Tue, 8 Jan 2013 23:56:33 +0100 Subject: [PATCH 07/13] Fixed code style --- src/Hydrator/Filter/FilterComposite.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index 00a99477a..cae986bc0 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -162,8 +162,8 @@ public function filter($property) $orCount = count($this->orFilter); // return true if no filters are registered if ( - $orCount === 0 && - $andCount === 0 + $orCount === 0 + && $andCount === 0 ) { return true; } elseif ($orCount === 0 && $andCount !== 0) { From bbbe6bf42d455e8835c324f7db3f6bf4c85e1786 Mon Sep 17 00:00:00 2001 From: ingo Date: Wed, 9 Jan 2013 00:37:25 +0100 Subject: [PATCH 08/13] Added filter to reflection --- src/Hydrator/Reflection.php | 3 +++ test/HydratorTest.php | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Hydrator/Reflection.php b/src/Hydrator/Reflection.php index b372665d1..d46abce7d 100644 --- a/src/Hydrator/Reflection.php +++ b/src/Hydrator/Reflection.php @@ -37,6 +37,9 @@ public function extract($object) $result = array(); foreach (self::getReflProperties($object) as $property) { $propertyName = $property->getName(); + if (!$this->filterComposite->filter($propertyName)) { + continue; + } $value = $property->getValue($object); $result[$propertyName] = $this->extractValue($propertyName, $value); diff --git a/test/HydratorTest.php b/test/HydratorTest.php index f51b09355..17ceef560 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -19,6 +19,7 @@ use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing; use ZendTest\Stdlib\TestAsset\Reflection as ReflectionAsset; +use ZendTest\Stdlib\TestAsset\ReflectionFilter; use ZendTest\Stdlib\TestAsset\ObjectProperty as ObjectPropertyAsset; use ZendTest\Stdlib\TestAsset\ArraySerializable as ArraySerializableAsset; use Zend\Stdlib\Hydrator\Strategy\DefaultStrategy; @@ -363,7 +364,8 @@ public function filterProvider() { return array( array(new ObjectProperty(), new ObjectPropertyAsset), - array(new ArraySerializable(), new ArraySerializableAsset) + array(new ArraySerializable(), new ArraySerializableAsset), + array(new Reflection(), new ReflectionFilter) ); } } From e822d1f216155ccaa50821ebdf212f21bd082b3a Mon Sep 17 00:00:00 2001 From: ingo Date: Wed, 9 Jan 2013 01:09:12 +0100 Subject: [PATCH 09/13] Added number of parameter filter. Fixed zendframework/zf2#3352 --- src/Hydrator/ClassMethods.php | 3 + src/Hydrator/Filter/FilterInterface.php | 7 +++ .../Filter/NumberOfParameterFilter.php | 60 +++++++++++++++++++ test/HydratorTest.php | 17 ++++++ .../ClassMethodsInvalidParameter.php | 43 +++++++++++++ test/TestAsset/ReflectionFilter.php | 35 +++++++++++ 6 files changed, 165 insertions(+) create mode 100644 src/Hydrator/Filter/NumberOfParameterFilter.php create mode 100644 test/TestAsset/ClassMethodsInvalidParameter.php create mode 100644 test/TestAsset/ReflectionFilter.php diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 4cb37c4e4..d78dfb847 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -11,9 +11,11 @@ namespace Zend\Stdlib\Hydrator; use Zend\Stdlib\Exception; +use Zend\Stdlib\Hydrator\Filter\FilterComposite; use Zend\Stdlib\Hydrator\Filter\GetFilter; use Zend\Stdlib\Hydrator\Filter\HasFilter; use Zend\Stdlib\Hydrator\Filter\IsFilter; +use Zend\Stdlib\Hydrator\Filter\NumberOfParameterFilter; /** * @category Zend @@ -40,6 +42,7 @@ public function __construct($underscoreSeparatedKeys = true) $this->filterComposite->addFilter("is", new IsFilter()); $this->filterComposite->addFilter("has", new HasFilter()); $this->filterComposite->addFilter("get", new GetFilter()); + $this->filterComposite->addFilter("parameter", new NumberOfParameterFilter(), FilterComposite::CONDITION_AND); } /** diff --git a/src/Hydrator/Filter/FilterInterface.php b/src/Hydrator/Filter/FilterInterface.php index 961ac6102..fe70feb84 100644 --- a/src/Hydrator/Filter/FilterInterface.php +++ b/src/Hydrator/Filter/FilterInterface.php @@ -16,5 +16,12 @@ */ interface FilterInterface { + /** + * Should return true, if the given filter + * does not match + * + * @param string $property The name of the property + * @return bool + */ public function filter($property); } diff --git a/src/Hydrator/Filter/NumberOfParameterFilter.php b/src/Hydrator/Filter/NumberOfParameterFilter.php new file mode 100644 index 000000000..2a0479b5a --- /dev/null +++ b/src/Hydrator/Filter/NumberOfParameterFilter.php @@ -0,0 +1,60 @@ +numberOfParameters = 0; + } + + /** + * @param string $property the name of the property + * @throws InvalidArgumentException + */ + public function filter($property) + { + try { + $reflectionMethod = new ReflectionMethod($property); + } catch( ReflectionException $exception) { + throw new InvalidArgumentException( + "Method $property doesn't exist" + ); + } + + if ($reflectionMethod->getNumberOfParameters() !== $this->numberOfParameters) { + return false; + } + + return true; + } +} + diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 17ceef560..261c6b146 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -18,6 +18,7 @@ use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase; use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing; +use ZendTest\Stdlib\TestAsset\ClassMethodsInvalidParameter; use ZendTest\Stdlib\TestAsset\Reflection as ReflectionAsset; use ZendTest\Stdlib\TestAsset\ReflectionFilter; use ZendTest\Stdlib\TestAsset\ObjectProperty as ObjectPropertyAsset; @@ -50,6 +51,11 @@ class HydratorTest extends \PHPUnit_Framework_TestCase */ protected $classMethodsUnderscore; + /** + * @var ClassMethodsInvalidParameter + */ + protected $classMethodsInvalidParameter; + /** * @var ReflectionAsset */ @@ -61,6 +67,7 @@ public function setUp() $this->classMethodsCamelCaseMissing = new ClassMethodsCamelCaseMissing(); $this->classMethodsUnderscore = new ClassMethodsUnderscore(); $this->reflection = new ReflectionAsset; + $this->classMethodsInvalidParameter = new ClassMethodsInvalidParameter(); } public function testInitiateValues() @@ -368,4 +375,14 @@ public function filterProvider() array(new Reflection(), new ReflectionFilter) ); } + + public function testHydratorClassMethodsWithInvalidNumberOfParameters() + { + $hydrator = new ClassMethods(false); + $datas = $hydrator->extract($this->classMethodsInvalidParameter); + + $this->assertTrue($datas['hasBar']); + $this->assertEquals('Bar', $datas['foo']); + $this->assertFalse($datas['isBla']); + } } diff --git a/test/TestAsset/ClassMethodsInvalidParameter.php b/test/TestAsset/ClassMethodsInvalidParameter.php new file mode 100644 index 000000000..cf9338865 --- /dev/null +++ b/test/TestAsset/ClassMethodsInvalidParameter.php @@ -0,0 +1,43 @@ +foo = "bar"; + $this->bar = "foo"; + $this->blubb = "baz"; + $this->quo = "blubb"; + } + +} + From 25fa34ad67c1bbb67eadcdabcceed53c72d803ad Mon Sep 17 00:00:00 2001 From: ingo Date: Fri, 11 Jan 2013 01:20:50 +0100 Subject: [PATCH 10/13] Implemented FilterProviderInterface and MethodMatchFilter --- src/Hydrator/ClassMethods.php | 19 ++++- .../Filter/FilterProviderInterface.php | 25 +++++++ src/Hydrator/Filter/MethodMatchFilter.php | 53 ++++++++++++++ test/HydratorTest.php | 11 +++ .../ClassMethodsFilterProviderInterface.php | 70 +++++++++++++++++++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/Hydrator/Filter/FilterProviderInterface.php create mode 100644 src/Hydrator/Filter/MethodMatchFilter.php create mode 100644 test/TestAsset/ClassMethodsFilterProviderInterface.php diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index d78dfb847..0c6ffb142 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -12,6 +12,8 @@ use Zend\Stdlib\Exception; use Zend\Stdlib\Hydrator\Filter\FilterComposite; +use Zend\Stdlib\Hydrator\Filter\FilterProviderInterface; +use Zend\Stdlib\Hydrator\Filter\MethodMatchFilter; use Zend\Stdlib\Hydrator\Filter\GetFilter; use Zend\Stdlib\Hydrator\Filter\HasFilter; use Zend\Stdlib\Hydrator\Filter\IsFilter; @@ -101,6 +103,21 @@ public function extract($object) )); } + $filter = null; + if ($object instanceof FilterProviderInterface) { + $filter = $object->getFilter(); + if ($filter instanceof FilterComposite) + { + $filter->addFilter( + "getfilter", + new MethodMatchFilter("getFilter"), + FilterComposite::CONDITION_AND + ); + } + } else { + $filter = $this->filterComposite; + } + $transform = function ($letters) { $letter = array_shift($letters); return '_' . strtolower($letter); @@ -110,7 +127,7 @@ public function extract($object) foreach ($methods as $method) { if ( - !$this->filterComposite->filter( + !$filter->filter( get_class($object) . '::' . $method ) ) { diff --git a/src/Hydrator/Filter/FilterProviderInterface.php b/src/Hydrator/Filter/FilterProviderInterface.php new file mode 100644 index 000000000..562b6fb6f --- /dev/null +++ b/src/Hydrator/Filter/FilterProviderInterface.php @@ -0,0 +1,25 @@ +method = $method; + $this->exclude = $exclude; + } + + public function filter($property) + { + $pos = strpos($property, '::'); + if ($pos !== false) { + $pos += 2; + } else { + $pos = 0; + } + if (substr($property, $pos) === $this->method) { + return $this->exclude ? false : true; + } + return $this->exclude ? true : false; + } +} diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 261c6b146..f67259c6f 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -16,6 +16,7 @@ use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Stdlib\Hydrator\Filter\FilterComposite; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase; +use ZendTest\Stdlib\TestAsset\ClassMethodsFilterProviderInterface; use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing; use ZendTest\Stdlib\TestAsset\ClassMethodsInvalidParameter; @@ -385,4 +386,14 @@ public function testHydratorClassMethodsWithInvalidNumberOfParameters() $this->assertEquals('Bar', $datas['foo']); $this->assertFalse($datas['isBla']); } + + public function testObjectBasedFilters() + { + $hydrator = new ClassMethods(false); + $foo = new ClassMethodsFilterProviderInterface(); + $data = $hydrator->extract($foo); + $this->assertFalse(array_key_exists("filter", $data)); + $this->assertSame("bar", $data["foo"]); + $this->assertSame("foo", $data["bar"]); + } } diff --git a/test/TestAsset/ClassMethodsFilterProviderInterface.php b/test/TestAsset/ClassMethodsFilterProviderInterface.php new file mode 100644 index 000000000..b24b8210a --- /dev/null +++ b/test/TestAsset/ClassMethodsFilterProviderInterface.php @@ -0,0 +1,70 @@ +addFilter("get", new GetFilter()); + $excludes = new FilterComposite(); + $excludes->addFilter( + "servicemanager", + new MethodMatchFilter("getServiceManager"), + FilterComposite::CONDITION_AND + ); + $excludes->addFilter( + "eventmanager", + new MethodMatchFilter("getEventManager"), + FilterComposite::CONDITION_AND + ); + $filterComposite->addFilter("excludes", $excludes, FilterComposite::CONDITION_AND); + + return $filterComposite; + } +} From 86896f80c9ff95c87e7f56619734a9307f021eea Mon Sep 17 00:00:00 2001 From: ingo Date: Fri, 11 Jan 2013 02:02:27 +0100 Subject: [PATCH 11/13] Simplified code --- src/Hydrator/ClassMethods.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 0c6ffb142..2255b232c 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -105,15 +105,10 @@ public function extract($object) $filter = null; if ($object instanceof FilterProviderInterface) { - $filter = $object->getFilter(); - if ($filter instanceof FilterComposite) - { - $filter->addFilter( - "getfilter", - new MethodMatchFilter("getFilter"), - FilterComposite::CONDITION_AND - ); - } + $filter = new FilterComposite( + array($object->getFilter()), + array(new MethodMatchFilter("getFilter")) + ); } else { $filter = $this->filterComposite; } From a9ddfc98c20c9ab629bb61826aa404146ad1a141 Mon Sep 17 00:00:00 2001 From: ingo Date: Sat, 12 Jan 2013 20:25:15 +0100 Subject: [PATCH 12/13] Removed dump --- src/Hydrator/ArraySerializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index da16c4981..8332a983b 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -46,7 +46,7 @@ public function extract($object) $value = $self->extractValue($name, $value); } }); - #var_dump($data); + return $data; } From 32ec458d09cc21f0d25ce2f1f3f68390ebcbae26 Mon Sep 17 00:00:00 2001 From: ingo Date: Sun, 13 Jan 2013 21:51:46 +0100 Subject: [PATCH 13/13] CS fixes --- src/Hydrator/ClassMethods.php | 14 +++++---- src/Hydrator/Filter/FilterComposite.php | 30 ++++++++----------- .../Filter/NumberOfParameterFilter.php | 1 - 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 2255b232c..42681737d 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -48,7 +48,7 @@ public function __construct($underscoreSeparatedKeys = true) } /** - * @param array|\Traversable $options + * @param array|\Traversable $options * @return ClassMethods * @throws Exception\InvalidArgumentException */ @@ -69,12 +69,13 @@ public function setOptions($options) } /** - * @param boolean $underscoreSeparatedKeys + * @param boolean $underscoreSeparatedKeys * @return ClassMethods */ public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys) { $this->underscoreSeparatedKeys = $underscoreSeparatedKeys; + return $this; } @@ -91,7 +92,7 @@ public function getUnderscoreSeparatedKeys() * * Extracts the getter/setter of the given $object. * - * @param object $object + * @param object $object * @return array * @throws Exception\BadMethodCallException for a non-object $object */ @@ -115,6 +116,7 @@ public function extract($object) $transform = function ($letters) { $letter = array_shift($letters); + return '_' . strtolower($letter); }; $attributes = array(); @@ -149,8 +151,8 @@ public function extract($object) * * Hydrates an object by getter/setter methods of the object. * - * @param array $data - * @param object $object + * @param array $data + * @param object $object * @return object * @throws Exception\BadMethodCallException for a non-object $object */ @@ -164,6 +166,7 @@ public function hydrate(array $data, $object) $transform = function ($letters) { $letter = substr(array_shift($letters), 1, 1); + return ucfirst($letter); }; @@ -178,6 +181,7 @@ public function hydrate(array $data, $object) $object->$method($value); } } + return $object; } diff --git a/src/Hydrator/Filter/FilterComposite.php b/src/Hydrator/Filter/FilterComposite.php index cae986bc0..7531d5bd1 100644 --- a/src/Hydrator/Filter/FilterComposite.php +++ b/src/Hydrator/Filter/FilterComposite.php @@ -48,7 +48,7 @@ public function __construct($orFilter = array(), $andFilter = array()) { array_walk($orFilter, function($value, $key) { - if( + if ( !is_callable($value) && !$value instanceof FilterInterface ) { @@ -62,7 +62,7 @@ function($value, $key) { array_walk($andFilter, function($value, $key) { - if( + if ( !is_callable($value) && !$value instanceof FilterInterface ) { @@ -94,9 +94,9 @@ function($value, $key) { * ); * * - * @param string $name - * @param callable|FilterInterface $filter - * @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND + * @param string $name + * @param callable|FilterInterface $filter + * @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND * @throws InvalidArgumentException * @return FilterComposite */ @@ -126,11 +126,11 @@ public function addFilter($name, $filter, $condition = self::CONDITION_OR) */ public function removeFilter($name) { - if( isset($this->orFilter[$name])) { + if (isset($this->orFilter[$name])) { unset($this->orFilter[$name]); } - if( isset($this->andFilter[$name])) { + if (isset($this->andFilter[$name])) { unset($this->andFilter[$name]); } @@ -161,10 +161,7 @@ public function filter($property) $andCount = count($this->andFilter); $orCount = count($this->orFilter); // return true if no filters are registered - if ( - $orCount === 0 - && $andCount === 0 - ) { + if ($orCount === 0 && $andCount === 0) { return true; } elseif ($orCount === 0 && $andCount !== 0) { $returnValue = true; @@ -173,10 +170,9 @@ public function filter($property) } // Check if 1 from the or filters return true - foreach($this->orFilter as $filter) { + foreach ($this->orFilter as $filter) { if (is_callable($filter)) { - if( $filter($property) === true) - { + if ( $filter($property) === true) { $returnValue = true; break; } @@ -190,14 +186,14 @@ public function filter($property) } // Check if all of the and condition return true - foreach($this->andFilter as $filter) { + foreach ($this->andFilter as $filter) { if (is_callable($filter)) { - if( $filter($property) === false) { + if ($filter($property) === false) { return false; } continue; } else { - if( $filter->filter($property) === false) { + if ($filter->filter($property) === false) { return false; } } diff --git a/src/Hydrator/Filter/NumberOfParameterFilter.php b/src/Hydrator/Filter/NumberOfParameterFilter.php index 2a0479b5a..3c2e0b94a 100644 --- a/src/Hydrator/Filter/NumberOfParameterFilter.php +++ b/src/Hydrator/Filter/NumberOfParameterFilter.php @@ -57,4 +57,3 @@ public function filter($property) return true; } } -