comparison vendor/wikimedia/composer-merge-plugin/src/Merge/NestedArray.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * This file is part of the Composer Merge plugin.
4 *
5 * Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
6 *
7 * This software may be modified and distributed under the terms of the MIT
8 * license. See the LICENSE file for details.
9 */
10
11 namespace Wikimedia\Composer\Merge;
12
13 /**
14 * Adapted from
15 * http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Component/Utility/NestedArray.php
16 * @ f86a4d650d5af0b82a3981e09977055fa63f6f2e
17 */
18 class NestedArray
19 {
20
21 /**
22 * Merges multiple arrays, recursively, and returns the merged array.
23 *
24 * This function is similar to PHP's array_merge_recursive() function, but
25 * it handles non-array values differently. When merging values that are
26 * not both arrays, the latter value replaces the former rather than
27 * merging with it.
28 *
29 * Example:
30 *
31 * @code
32 * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b')));
33 * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd')));
34 *
35 * // This results in array('fragment' => array('x', 'y'), 'attributes' =>
36 * // array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b',
37 * // 'c', 'd'))).
38 * $incorrect = array_merge_recursive($link_options_1, $link_options_2);
39 *
40 * // This results in array('fragment' => 'y', 'attributes' =>
41 * // array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))).
42 * $correct = NestedArray::mergeDeep($link_options_1, $link_options_2);
43 * @endcode
44 *
45 * @param array ...
46 * Arrays to merge.
47 *
48 * @return array
49 * The merged array.
50 *
51 * @see NestedArray::mergeDeepArray()
52 */
53 public static function mergeDeep()
54 {
55 return self::mergeDeepArray(func_get_args());
56 }
57
58 /**
59 * Merges multiple arrays, recursively, and returns the merged array.
60 *
61 * This function is equivalent to NestedArray::mergeDeep(), except the
62 * input arrays are passed as a single array parameter rather than
63 * a variable parameter list.
64 *
65 * The following are equivalent:
66 * - NestedArray::mergeDeep($a, $b);
67 * - NestedArray::mergeDeepArray(array($a, $b));
68 *
69 * The following are also equivalent:
70 * - call_user_func_array('NestedArray::mergeDeep', $arrays_to_merge);
71 * - NestedArray::mergeDeepArray($arrays_to_merge);
72 *
73 * @param array $arrays
74 * An arrays of arrays to merge.
75 * @param bool $preserveIntegerKeys
76 * (optional) If given, integer keys will be preserved and merged
77 * instead of appended. Defaults to false.
78 *
79 * @return array
80 * The merged array.
81 *
82 * @see NestedArray::mergeDeep()
83 */
84 public static function mergeDeepArray(
85 array $arrays,
86 $preserveIntegerKeys = false
87 ) {
88 $result = array();
89 foreach ($arrays as $array) {
90 foreach ($array as $key => $value) {
91 // Renumber integer keys as array_merge_recursive() does
92 // unless $preserveIntegerKeys is set to TRUE. Note that PHP
93 // automatically converts array keys that are integer strings
94 // (e.g., '1') to integers.
95 if (is_integer($key) && !$preserveIntegerKeys) {
96 $result[] = $value;
97 } elseif (isset($result[$key]) &&
98 is_array($result[$key]) &&
99 is_array($value)
100 ) {
101 // Recurse when both values are arrays.
102 $result[$key] = self::mergeDeepArray(
103 array($result[$key], $value),
104 $preserveIntegerKeys
105 );
106 } else {
107 // Otherwise, use the latter value, overriding any
108 // previous value.
109 $result[$key] = $value;
110 }
111 }
112 }
113 return $result;
114 }
115 }
116 // vim:sw=4:ts=4:sts=4:et: