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

Commit

Permalink
Merge branch 'feature/6962' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Feb 24, 2015
6 parents 2d7b3ea + 7ea6197 + eb8d23e + a349d33 + 87e12cd + 94bcc86 commit 67e9c62
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/Blacklist.php
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;
}
}
2 changes: 2 additions & 0 deletions src/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FilterPluginManager extends AbstractPluginManager
'alnum' => 'Zend\I18n\Filter\Alnum',
'alpha' => 'Zend\I18n\Filter\Alpha',
'basename' => 'Zend\Filter\BaseName',
'blacklist' => 'Zend\Filter\Blacklist',
'boolean' => 'Zend\Filter\Boolean',
'callback' => 'Zend\Filter\Callback',
'compress' => 'Zend\Filter\Compress',
Expand Down Expand Up @@ -76,6 +77,7 @@ class FilterPluginManager extends AbstractPluginManager
'stripnewlines' => 'Zend\Filter\StripNewlines',
'striptags' => 'Zend\Filter\StripTags',
'urinormalize' => 'Zend\Filter\UriNormalize',
'whitelist' => 'Zend\Filter\Whitelist',
'wordcamelcasetodash' => 'Zend\Filter\Word\CamelCaseToDash',
'wordcamelcasetoseparator' => 'Zend\Filter\Word\CamelCaseToSeparator',
'wordcamelcasetounderscore' => 'Zend\Filter\Word\CamelCaseToUnderscore',
Expand Down
91 changes: 91 additions & 0 deletions src/Whitelist.php
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;
}
}
150 changes: 150 additions & 0 deletions test/BlacklistTest.php
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()),
),
),
);
}
}
Loading

0 comments on commit 67e9c62

Please sign in to comment.