Updates according to suggestions.
- Simplified logic of tests. - Added more comments/docblocks. - Added more convenience.
This commit is contained in:
parent
6aec7898e3
commit
c4a0f799af
@ -250,17 +250,26 @@ class ParameterBag
|
||||
* @param mixed $default Default = null.
|
||||
* @param boolean $deep Default = false.
|
||||
* @param integer $filter FILTER_* constant.
|
||||
* @param array $options Filter options - can be an array of options or FILTER_* constant.
|
||||
* @param mixed $options Filter options.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.filter-var.php
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, array $options=array())
|
||||
public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array())
|
||||
{
|
||||
$value = $this->get($key, $default, $deep);
|
||||
if (is_array($value)) {
|
||||
|
||||
// Always turn $options into an array - this allows filter_var option shortcuts.
|
||||
if (!is_array($options) && $options) {
|
||||
$options = array('flags' => $options);
|
||||
}
|
||||
|
||||
// Add a convenience check for arrays.
|
||||
if (is_array($value) && !isset($options['flags'])) {
|
||||
$options['flags'] = FILTER_REQUIRE_ARRAY;
|
||||
}
|
||||
|
||||
|
||||
return filter_var($value, $filter, $options);
|
||||
}
|
||||
}
|
||||
|
@ -171,10 +171,10 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
|
||||
$bag = new ParameterBag(array(
|
||||
'digits' => '0123ab',
|
||||
'email' => 'example@example.com',
|
||||
'url' => 'http://example.com/',
|
||||
'url' => 'http://example.com/foo',
|
||||
'dec' => '256',
|
||||
'hex' => '0x100',
|
||||
'array' => 'bang',
|
||||
'array' => array('bang'),
|
||||
));
|
||||
|
||||
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
|
||||
@ -183,7 +183,10 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
|
||||
|
||||
$this->assertNotEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as url with a path');
|
||||
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as url with a path');
|
||||
|
||||
// This test is repeated for code-coverage
|
||||
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as url with a path');
|
||||
|
||||
$this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
|
||||
'flags' => FILTER_FLAG_ALLOW_HEX,
|
||||
@ -195,6 +198,8 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
|
||||
'options' => array('min_range' => 1, 'max_range' => 0xff))
|
||||
), '->filter() gets a value of parameter as integer between boundaries');
|
||||
|
||||
$this->assertNotEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
|
||||
$this->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user