From 6e2ffafe54862710d3ae27e726de058b70b83a27 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 5 Jan 2011 22:48:09 -0500 Subject: [PATCH] Fixing Set::filter() not predictably filtering in a recursive fashion. While a minor change in behavior, more predictable and uniform behavior is worth it. Fixes #1431 --- cake/libs/set.php | 16 ++++++++++++++-- cake/tests/cases/libs/set.test.php | 14 +++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 3d4e89f02..289c5af59 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -68,10 +68,22 @@ class Set { * @static */ function filter($var, $isArray = false) { - if (is_array($var) && (!empty($var) || $isArray)) { - return array_filter($var, array('Set', 'filter')); + foreach ((array)$var as $k => $v) { + if (!empty($v) && is_array($v)) { + $var[$k] = array_filter($v, array('Set', '_filter')); + } } + return array_filter($var, array('Set', '_filter')); + } +/** + * Set::filter callback function + * + * @param array $var Array to filter. + * @return boolean + * @access protected + */ + function _filter($var) { if ($var === 0 || $var === '0' || !empty($var)) { return true; } diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 3018c351a..9f0d9a596 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -88,8 +88,20 @@ class SetTest extends CakeTestCase { */ function testFilter() { $result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false))); - $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be', false)); + $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be')); $this->assertIdentical($result, $expected); + + $result = Set::filter(array(1, array(false))); + $expected = array(1); + $this->assertEqual($expected, $result); + + $result = Set::filter(array(1, array(false, false))); + $expected = array(1); + $this->assertEqual($expected, $result); + + $result = Set::filter(array(1, array('empty', false))); + $expected = array(1, array('empty')); + $this->assertEqual($expected, $result); } /**