Skip to content

Commit 5b4b936

Browse files
committed
Make filter() more flexible.
1 parent 239f52c commit 5b4b936

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

lib/Cake/Utility/Set2.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Set2 {
3636
* but is faster for simple operations.
3737
*
3838
* @param array $data Array of data to operate on.
39-
* @param string $path The path being searched for.
39+
* @param mixed $path The path being searched for. Either a dot
40+
* separated string, or an array of path segments.
4041
* @return mixed The value fetched from the array, or null.
4142
*/
4243
public static function get(array $data, $path) {
@@ -484,19 +485,21 @@ public static function check(array $data, $path) {
484485
}
485486

486487
/**
487-
* Recursively filters empty elements out of a route array, excluding '0'.
488+
* Recursively filters a data set.
488489
*
489490
* @param array $data Either an array to filter, or value when in callback
491+
* @param callable $callback A function to filter the data with. Defaults to
492+
* `self::_filter()` Which strips out all non-zero empty values.
490493
* @return array Filtered array
491494
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
492495
*/
493-
public static function filter(array $data) {
496+
public static function filter(array $data, $callback = array('self', '_filter')) {
494497
foreach ($data as $k => $v) {
495498
if (is_array($v)) {
496-
$data[$k] = self::filter($v);
499+
$data[$k] = self::filter($v, $callback);
497500
}
498501
}
499-
return array_filter($data, array('self', '_filter'));
502+
return array_filter($data, $callback);
500503
}
501504

502505
/**
@@ -817,16 +820,21 @@ public static function normalize(array $data, $assoc = true) {
817820
/**
818821
* Takes in a flat array and returns a nested array
819822
*
820-
* @param mixed $data
823+
* ### Options:
824+
*
825+
* - `children` The key name to use in the resultset for children.
826+
* - `idPath` The path to a key that identifies each entry. Should be
827+
* compatible with Set2::extract(). Defaults to `{n}.$alias.id`
828+
* - `parentPath` The path to a key that identifies the parent of each entry.
829+
* Should be compatible with Set2::extract(). Defaults to `{n}.$alias.parent_id`
830+
* - `root` The id of the desired top-most result.
831+
*
832+
* @param mixed $data The data to nest.
821833
* @param array $options Options are:
822-
* children - the key name to use in the resultset for children
823-
* idPath - the path to a key that identifies each entry
824-
* parentPath - the path to a key that identifies the parent of each entry
825-
* root - the id of the desired top-most result
826834
* @return array of results, nested
827-
* @link
835+
* @see Set::extract()
828836
*/
829-
public static function nest($data, $options = array()) {
837+
public static function nest(array $data, $options = array()) {
830838
if (!$data) {
831839
return $data;
832840
}

0 commit comments

Comments
 (0)