Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Added number of parameter filter. Fixed #3352
Browse files Browse the repository at this point in the history
  • Loading branch information
iwalz committed Jan 9, 2013
1 parent b185085 commit 82dac94
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/Zend/Stdlib/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions library/Zend/Stdlib/Hydrator/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
60 changes: 60 additions & 0 deletions library/Zend/Stdlib/Hydrator/Filter/NumberOfParameterFilter.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

17 changes: 17 additions & 0 deletions tests/ZendTest/Stdlib/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,11 @@ class HydratorTest extends \PHPUnit_Framework_TestCase
*/
protected $classMethodsUnderscore;

/**
* @var ClassMethodsInvalidParameter
*/
protected $classMethodsInvalidParameter;

/**
* @var ReflectionAsset
*/
Expand All @@ -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()
Expand Down Expand Up @@ -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']);
}
}
43 changes: 43 additions & 0 deletions tests/ZendTest/Stdlib/TestAsset/ClassMethodsInvalidParameter.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
35 changes: 35 additions & 0 deletions tests/ZendTest/Stdlib/TestAsset/ReflectionFilter.php
Original file line number Diff line number Diff line change
@@ -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.