Making Set::filter() only operate on arrays.

Also making Set::filter() work properly in a recursive fashion.  This matches behavior with other functions in Set.  Fixes #1431
This commit is contained in:
mark_story 2011-01-09 22:34:36 -05:00
parent f87ae5469b
commit 166c776099
2 changed files with 11 additions and 4 deletions

View file

@ -61,10 +61,10 @@ class Set {
* @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'));

View file

@ -100,8 +100,15 @@ class SetTest extends CakeTestCase {
$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
*