[TASK] Remove XCLASS definitions from cache classes
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_array.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Susanne Moog <typo3@susanne-moog.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Class with helper functions for array handling
30 *
31 * @author Susanne Moog <typo3@susanne-moog.de>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 final class t3lib_utility_Array {
36
37 /**
38 * Reduce an array by a search value and keep the array structure.
39 *
40 * Comparison is type strict:
41 * - For a given needle of type string, integer, array or boolean,
42 * value and value type must match to occur in result array
43 * - For a given object, a object within the array must be a reference to
44 * the same object to match (not just different instance of same class)
45 *
46 * Example:
47 * - Needle: 'findMe'
48 * - Given array:
49 * array(
50 * 'foo' => 'noMatch',
51 * 'bar' => 'findMe',
52 * 'foobar => array(
53 * 'foo' => 'findMe',
54 * ),
55 * );
56 * - Result:
57 * array(
58 * 'bar' => 'findMe',
59 * 'foobar' => array(
60 * 'foo' => findMe',
61 * ),
62 * );
63 *
64 * See the unit tests for more examples and expected behaviour
65 *
66 * @static
67 * @param mixed $needle The value to search for
68 * @param array $haystack The array in which to search
69 * @return array $haystack array reduced matching $needle values
70 */
71 public static function filterByValueRecursive($needle = '', array $haystack = array()) {
72 $resultArray = array();
73
74 // Define a lambda function to be applied to all members of this array dimension
75 // Call recursive if current value is of type array
76 // Write to $resultArray (by reference!) if types and value match
77 $callback = function(&$value, $key) use ($needle, &$resultArray) {
78 if ($value === $needle) {
79 $resultArray[$key] = $value;
80 } elseif (is_array($value)) {
81 // self does not work in lambda functions, use t3lib_utility_Array for recursion
82 $subArrayMatches = t3lib_utility_Array::filterByValueRecursive($needle, $value, $array[$key]);
83 if (count($subArrayMatches) > 0) {
84 $resultArray[$key] = $subArrayMatches;
85 }
86 }
87 };
88
89 // array_walk() is not affected by the internal pointers, no need to reset
90 array_walk($haystack, $callback);
91
92 // Pointers to result array are reset internally
93 return $resultArray;
94 }
95 }
96
97 ?>