Pull merge.
[yaffs-website] / vendor / consolidation / config / src / Util / ArrayUtil.php
1 <?php
2 namespace Consolidation\Config\Util;
3
4 /**
5  * Useful array utilities.
6  */
7 class ArrayUtil
8 {
9     /**
10      * Merges arrays recursively while preserving.
11      *
12      * @param array $array1
13      * @param array $array2
14      *
15      * @return array
16      *
17      * @see http://php.net/manual/en/function.array-merge-recursive.php#92195
18      * @see https://github.com/grasmash/bolt/blob/robo-rebase/src/Robo/Common/ArrayManipulator.php#L22
19      */
20     public static function mergeRecursiveDistinct(
21         array &$array1,
22         array &$array2
23     ) {
24         $merged = $array1;
25         foreach ($array2 as $key => &$value) {
26             $merged[$key] = self::mergeRecursiveValue($merged, $key, $value);
27         }
28         return $merged;
29     }
30
31     /**
32      * Process the value in an mergeRecursiveDistinct - make a recursive
33      * call if needed.
34      */
35     protected static function mergeRecursiveValue(&$merged, $key, $value)
36     {
37         if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
38             return self::mergeRecursiveDistinct($merged[$key], $value);
39         }
40         return $value;
41     }
42
43
44     /**
45      * Merges arrays recursively while preserving.
46      *
47      * @param array $array1
48      * @param array $array2
49      *
50      * @return array
51      *
52      * @see http://php.net/manual/en/function.array-merge-recursive.php#92195
53      * @see https://github.com/grasmash/bolt/blob/robo-rebase/src/Robo/Common/ArrayManipulator.php#L22
54      */
55     public static function mergeRecursiveSelect(
56         array &$array1,
57         array &$array2,
58         array $selectionList,
59         $keyPrefix = ''
60     ) {
61         $merged = $array1;
62         foreach ($array2 as $key => &$value) {
63             $merged[$key] = self::mergeRecursiveSelectValue($merged, $key, $value, $selectionList, $keyPrefix);
64         }
65         return $merged;
66     }
67
68     /**
69      * Process the value in an mergeRecursiveDistinct - make a recursive
70      * call if needed.
71      */
72     protected static function mergeRecursiveSelectValue(&$merged, $key, $value, $selectionList, $keyPrefix)
73     {
74         if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
75             if (self::selectMerge($keyPrefix, $key, $selectionList)) {
76                 return array_merge_recursive($merged[$key], $value);
77             } else {
78                 return self::mergeRecursiveSelect($merged[$key], $value, $selectionList, "${keyPrefix}${key}.");
79             }
80         }
81         return $value;
82     }
83
84     protected static function selectMerge($keyPrefix, $key, $selectionList)
85     {
86         return in_array("${keyPrefix}${key}", $selectionList);
87     }
88
89
90     /**
91      * Fills all of the leaf-node values of a nested array with the
92      * provided replacement value.
93      */
94     public static function fillRecursive(array $data, $fill)
95     {
96         $result = [];
97         foreach ($data as $key => $value) {
98             $result[$key] = $fill;
99             if (self::isAssociative($value)) {
100                 $result[$key] = self::fillRecursive($value, $fill);
101             }
102         }
103         return $result;
104     }
105
106     /**
107      * Return true if the provided parameter is an array, and at least
108      * one key is non-numeric.
109      */
110     public static function isAssociative($testArray)
111     {
112         if (!is_array($testArray)) {
113             return false;
114         }
115         foreach (array_keys($testArray) as $key) {
116             if (!is_numeric($key)) {
117                 return true;
118             }
119         }
120         return false;
121     }
122 }