Skip to content

Commit

Permalink
Making Set::filter() only operate on arrays.
Browse files Browse the repository at this point in the history
Also making Set::filter() work properly in a recursive fashion.  This matches behavior with other functions in Set.  Fixes #1431
  • Loading branch information
markstory committed Jan 10, 2011
1 parent f87ae54 commit 166c776
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cake/libs/set.php
Expand Up @@ -61,10 +61,10 @@ public static function merge($arr1, $arr2 = null) {
* @param boolean $isArray Force to tell $var is an array when $var is empty
* @return mixed Either filtered array, or true/false when in callback
*/
public static function filter($var, $isArray = false) {
foreach ((array)$var as $k => $v) {
if (!empty($v) && is_array($v)) {
$var[$k] = array_filter($v, array('Set', '_filter'));
public static function filter(array $var) {
foreach ($var as $k => $v) {
if (is_array($v)) {
$var[$k] = Set::filter($v);
}
}
return array_filter($var, array('Set', '_filter'));
Expand Down
7 changes: 7 additions & 0 deletions cake/tests/cases/libs/set.test.php
Expand Up @@ -100,8 +100,15 @@ function testFilter() {
$result = Set::filter(array(1, array('empty', false)));
$expected = array(1, array('empty'));
$this->assertEqual($expected, $result);

$result = Set::filter(array(1, array('2', false, array(3, null))));
$expected = array(1, array('2', 2 => array(3)));
$this->assertEqual($expected, $result);

$this->assertSame(array(), Set::filter(array()));
}


/**
* testNumericArrayCheck method
*
Expand Down

0 comments on commit 166c776

Please sign in to comment.