Debian Codegraph @

vendor/wikimedia/composer-merge-plugin/src/NestedArray.php

raw
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\V2;
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 = ['fragment' => 'x', 'attributes' => ['title' => t('X'), 'class' => ['a', 'b']]];
33 * $link_options_2 = ['fragment' => 'y', 'attributes' => ['title' => t('Y'), 'class' => ['c', 'd']]];
34 *
35 * // This results in ['fragment' => ['x', 'y'], 'attributes' =>
36 * // ['title' => [t('X'), t('Y')], 'class' => ['a', 'b',
37 * // 'c', 'd']]].
38 * $incorrect = array_merge_recursive($link_options_1, $link_options_2);
39 *
40 * // This results in ['fragment' => 'y', 'attributes' =>
41 * // ['title' => t('Y'), 'class' => ['a', 'b', 'c', 'd']]].
42 * $correct = NestedArray::mergeDeep($link_options_1, $link_options_2);
43 * @endcode
44 *
45 * @param mixed ...$params Arrays to merge.
46 *
47 * @return array The merged array.
48 *
49 * @see NestedArray::mergeDeepArray()
50 */
51 public static function mergeDeep(...$params)
52 {
53 return self::mergeDeepArray($params);
54 }
55
56 /**
57 * Merges multiple arrays, recursively, and returns the merged array.
58 *
59 * This function is equivalent to NestedArray::mergeDeep(), except the
60 * input arrays are passed as a single array parameter rather than
61 * a variable parameter list.
62 *
63 * The following are equivalent:
64 * - NestedArray::mergeDeep($a, $b);
65 * - NestedArray::mergeDeepArray([$a, $b]);
66 *
67 * The following are also equivalent:
68 * - call_user_func_array('NestedArray::mergeDeep', $arrays_to_merge);
69 * - NestedArray::mergeDeepArray($arrays_to_merge);
70 *
71 * @param array $arrays
72 * An arrays of arrays to merge.
73 * @param bool $preserveIntegerKeys
74 * (optional) If given, integer keys will be preserved and merged
75 * instead of appended. Defaults to false.
76 *
77 * @return array
78 * The merged array.
79 *
80 * @see NestedArray::mergeDeep()
81 */
82 public static function mergeDeepArray(
83 array $arrays,
84 $preserveIntegerKeys = false
85 ) {
86 $result = [];
87 foreach ($arrays as $array) {
88 foreach ($array as $key => $value) {
89 // Renumber integer keys as array_merge_recursive() does
90 // unless $preserveIntegerKeys is set to TRUE. Note that PHP
91 // automatically converts array keys that are integer strings
92 // (e.g., '1') to integers.
93 if (is_int($key) && !$preserveIntegerKeys) {
94 $result[] = $value;
95 } elseif (isset($result[$key]) &&
96 is_array($result[$key]) &&
97 is_array($value)
98 ) {
99 // Recurse when both values are arrays.
100 $result[$key] = self::mergeDeepArray(
101 [$result[$key], $value],
102 $preserveIntegerKeys
103 );
104 } else {
105 // Otherwise, use the latter value, overriding any
106 // previous value.
107 $result[$key] = $value;
108 }
109 }
110 }
111 return $result;
112 }
113 }
114 // vim:sw=4:ts=4:sts=4:et: