This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/6962' into develop
- Loading branch information
Showing
5 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
use Traversable; | ||
use Zend\Stdlib\ArrayUtils; | ||
|
||
class Blacklist extends AbstractFilter | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $strict = false; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $list = array(); | ||
|
||
/** | ||
* @param null|array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if (null !== $options) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Determine whether the in_array() call should be "strict" or not. See in_array docs. | ||
* | ||
* @param bool $strict | ||
*/ | ||
public function setStrict($strict = true) | ||
{ | ||
$this->strict = (bool) $strict; | ||
} | ||
|
||
/** | ||
* Returns whether the in_array() call should be "strict" or not. See in_array docs. | ||
* | ||
* @return boolean | ||
*/ | ||
public function getStrict() | ||
{ | ||
return $this->strict; | ||
} | ||
|
||
/** | ||
* Set the list of items to black-list. | ||
* | ||
* @param array|Traversable $list | ||
*/ | ||
public function setList($list = array()) | ||
{ | ||
if (!is_array($list)) { | ||
$list = ArrayUtils::iteratorToArray($list); | ||
} | ||
|
||
$this->list = $list; | ||
} | ||
|
||
/** | ||
* Get the list of items to black-list | ||
* | ||
* @return array | ||
*/ | ||
public function getList() | ||
{ | ||
return $this->list; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* Will return null if $value is present in the black-list. If $value is NOT present then it will return $value. | ||
*/ | ||
public function filter($value) | ||
{ | ||
return in_array($value, $this->getList(), $this->getStrict()) ? null : $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
use Traversable; | ||
use Zend\Stdlib\ArrayUtils; | ||
|
||
class Whitelist extends AbstractFilter | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $strict = false; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $list = array(); | ||
|
||
/** | ||
* @param null|array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if (null !== $options) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Determine whether the in_array() call should be "strict" or not. See in_array docs. | ||
* | ||
* @param bool $strict | ||
*/ | ||
public function setStrict($strict = true) | ||
{ | ||
$this->strict = (bool) $strict; | ||
} | ||
|
||
/** | ||
* Returns whether the in_array() call should be "strict" or not. See in_array docs. | ||
* | ||
* @return boolean | ||
*/ | ||
public function getStrict() | ||
{ | ||
return $this->strict; | ||
} | ||
|
||
/** | ||
* Set the list of items to white-list. | ||
* | ||
* @param array|Traversable $list | ||
*/ | ||
public function setList($list = array()) | ||
{ | ||
if (!is_array($list)) { | ||
$list = ArrayUtils::iteratorToArray($list); | ||
} | ||
|
||
$this->list = $list; | ||
} | ||
|
||
|
||
/** | ||
* Get the list of items to white-list | ||
* | ||
* @return array | ||
*/ | ||
public function getList() | ||
{ | ||
return $this->list; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* Will return $value if its present in the white-list. If $value is rejected then it will return null. | ||
*/ | ||
public function filter($value) | ||
{ | ||
return in_array($value, $this->getList(), $this->getStrict()) ? $value : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use Zend\Filter\FilterPluginManager; | ||
use Zend\Filter\Blacklist as BlacklistFilter; | ||
use Zend\Stdlib\ArrayObject; | ||
|
||
/** | ||
* @group Zend_Filter | ||
*/ | ||
class BlacklistTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructorOptions() | ||
{ | ||
$filter = new BlacklistFilter(array( | ||
'list' => array('test', 1), | ||
'strict' => true, | ||
)); | ||
|
||
$this->assertEquals(true, $filter->getStrict()); | ||
$this->assertEquals(array('test', 1), $filter->getList()); | ||
} | ||
|
||
public function testConstructorDefaults() | ||
{ | ||
$filter = new BlacklistFilter(); | ||
|
||
$this->assertEquals(false, $filter->getStrict()); | ||
$this->assertEquals(array(), $filter->getList()); | ||
} | ||
|
||
public function testWithPluginManager() | ||
{ | ||
$pluginManager = new FilterPluginManager(); | ||
$filter = $pluginManager->get('blacklist'); | ||
|
||
$this->assertInstanceOf('Zend\Filter\Blacklist', $filter); | ||
} | ||
|
||
public function testNullListShouldThrowException() | ||
{ | ||
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException'); | ||
$filter = new BlacklistFilter(array( | ||
'list' => null, | ||
)); | ||
} | ||
|
||
public function testTraversableConvertsToArray() | ||
{ | ||
$array = array('test', 1); | ||
$obj = new ArrayObject(array('test', 1)); | ||
$filter = new BlacklistFilter(array( | ||
'list' => $obj, | ||
)); | ||
$this->assertEquals($array, $filter->getList()); | ||
} | ||
|
||
public function testSetStrictShouldCastToBoolean() | ||
{ | ||
$filter = new BlacklistFilter(array( | ||
'strict' => 1 | ||
)); | ||
$this->assertSame(true, $filter->getStrict()); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param bool $expected | ||
* @dataProvider defaultTestProvider | ||
*/ | ||
public function testDefault($value, $expected) | ||
{ | ||
$filter = new BlacklistFilter(); | ||
$this->assertSame($expected, $filter->filter($value)); | ||
} | ||
|
||
/** | ||
* @param bool $strict | ||
* @param array $testData | ||
* @dataProvider listTestProvider | ||
*/ | ||
public function testList($strict, $list, $testData) | ||
{ | ||
$filter = new BlacklistFilter(array( | ||
'strict' => $strict, | ||
'list' => $list, | ||
)); | ||
foreach ($testData as $data) { | ||
list($value, $expected) = $data; | ||
$message = sprintf( | ||
'%s (%s) is not filtered as %s; type = %s', | ||
var_export($value, true), | ||
gettype($value), | ||
var_export($expected, true), | ||
$strict | ||
); | ||
$this->assertSame($expected, $filter->filter($value), $message); | ||
} | ||
} | ||
|
||
public static function defaultTestProvider() | ||
{ | ||
return array( | ||
array('test', 'test'), | ||
array(0, 0), | ||
array(0.1, 0.1), | ||
array(array(), array()), | ||
array(null, null), | ||
); | ||
} | ||
|
||
public static function listTestProvider() | ||
{ | ||
return array( | ||
array( | ||
true, //strict | ||
array('test', 0), | ||
array( | ||
array('test', null), | ||
array(0, null), | ||
array(null, null), | ||
array(false, false), | ||
array(0.0, 0.0), | ||
array(array(), array()), | ||
), | ||
), | ||
array( | ||
false, //not strict | ||
array('test', 0), | ||
array( | ||
array('test', null), | ||
array(0, null), | ||
array(null, null), | ||
array(false, null), | ||
array(0.0, null), | ||
array(0.1, 0.1), | ||
array(array(), array()), | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.