Permalink
Browse files

Added number of parameter filter. Fixed #3352

  • Loading branch information...
1 parent b185085 commit 82dac94e00b6af44347dacc2bb6c70a2d165011b @iwalz iwalz committed Jan 9, 2013
@@ -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);
}
/**
@@ -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);
}
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Service
+ */
+
+namespace Zend\Stdlib\Hydrator\Filter;
+
+use ReflectionMethod;
+use ReflectionException;
+use Zend\Stdlib\Exception\InvalidArgumentException;
+use Zend\Stdlib\Hydrator\Filter\FilterInterface;
+
+/**
+ * @category Zend
+ * @package Zend_Stdlib
+ * @subpackage Hydrator
+ */
+class NumberOfParameterFilter implements FilterInterface
+{
+ /**
+ * The number of parameters beeing accepted
+ * @var int
+ */
+ protected $numberOfParameters = null;
+
+ /**
+ * @param int $numberOfParameters Number of accepted parameters
+ */
+ public function __construct($numberOfParameters = 0)
+ {
+ $this->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;
+ }
+}
+
@@ -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;
@@ -51,6 +52,11 @@ class HydratorTest extends \PHPUnit_Framework_TestCase
protected $classMethodsUnderscore;
/**
+ * @var ClassMethodsInvalidParameter
+ */
+ protected $classMethodsInvalidParameter;
+
+ /**
* @var ReflectionAsset
*/
protected $reflection;
@@ -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']);
+ }
}
@@ -0,0 +1,43 @@
+<?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;
+
+class ClassMethodsInvalidParameter
+{
+ public function hasAlias($alias)
+ {
+ return $alias;
+ }
+
+ public function getTest($foo)
+ {
+ return $foo;
+ }
+
+ public function isTest($bar)
+ {
+ return $bar;
+ }
+
+ public function hasBar()
+ {
+ return true;
+ }
+
+ public function getFoo()
+ {
+ return "Bar";
+ }
+
+ public function isBla()
+ {
+ return false;
+ }
+}
@@ -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 ReflectionFilter
+{
+ protected $foo = null;
+ protected $bar = null;
+ protected $blubb = null;
+ protected $quo = null;
+
+ public function __construct()
+ {
+ $this->foo = "bar";
+ $this->bar = "foo";
+ $this->blubb = "baz";
+ $this->quo = "blubb";
+ }
+
+}
+

0 comments on commit 82dac94

Please sign in to comment.