Skip to content

Commit c4a0f79

Browse files
author
Drak
committed
Updates according to suggestions.
- Simplified logic of tests. - Added more comments/docblocks. - Added more convenience.
1 parent 6aec789 commit c4a0f79

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,26 @@ public function getInt($key, $default = 0, $deep = false)
250250
* @param mixed $default Default = null.
251251
* @param boolean $deep Default = false.
252252
* @param integer $filter FILTER_* constant.
253-
* @param array $options Filter options - can be an array of options or FILTER_* constant.
253+
* @param mixed $options Filter options.
254+
*
255+
* @see http://php.net/manual/en/function.filter-var.php
254256
*
255257
* @return mixed
256258
*/
257-
public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, array $options=array())
259+
public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array())
258260
{
259261
$value = $this->get($key, $default, $deep);
260-
if (is_array($value)) {
262+
263+
// Always turn $options into an array - this allows filter_var option shortcuts.
264+
if (!is_array($options) && $options) {
265+
$options = array('flags' => $options);
266+
}
267+
268+
// Add a convenience check for arrays.
269+
if (is_array($value) && !isset($options['flags'])) {
261270
$options['flags'] = FILTER_REQUIRE_ARRAY;
262271
}
263-
272+
264273
return filter_var($value, $filter, $options);
265274
}
266275
}

tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ public function testFilter()
171171
$bag = new ParameterBag(array(
172172
'digits' => '0123ab',
173173
'email' => 'example@example.com',
174-
'url' => 'http://example.com/',
174+
'url' => 'http://example.com/foo',
175175
'dec' => '256',
176176
'hex' => '0x100',
177-
'array' => 'bang',
177+
'array' => array('bang'),
178178
));
179179

180180
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
@@ -183,7 +183,10 @@ public function testFilter()
183183

184184
$this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
185185

186-
$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');
186+
$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');
187+
188+
// This test is repeated for code-coverage
189+
$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');
187190

188191
$this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
189192
'flags' => FILTER_FLAG_ALLOW_HEX,
@@ -195,6 +198,8 @@ public function testFilter()
195198
'options' => array('min_range' => 1, 'max_range' => 0xff))
196199
), '->filter() gets a value of parameter as integer between boundaries');
197200

198-
$this->assertNotEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
201+
$this->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
202+
203+
199204
}
200205
}

0 commit comments

Comments
 (0)