Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
MergeRecursiveTrait | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
11 | |
100.00% |
1 / 1 |
isStringKeyedArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
mergeRecursive | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
9 |
1 | <?php |
2 | |
3 | /** |
4 | * VuFind Merge Recursive Trait - Provides Custom Array Merge Function |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2024 |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Feature |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @author Chris Hallberg <challber@villanova.edu> |
27 | * @author Thomas Wagener <wagener@hebis.uni-frankfurt.de> |
28 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
29 | * @link https://vufind.org Main Page |
30 | */ |
31 | |
32 | namespace VuFind\Feature; |
33 | |
34 | use function array_key_exists; |
35 | use function is_array; |
36 | |
37 | /** |
38 | * VuFind Merge Recursive Trait - Provides Custom Array Merge Function |
39 | * |
40 | * @category VuFind |
41 | * @package Feature |
42 | * @author Demian Katz <demian.katz@villanova.edu> |
43 | * @author Chris Hallberg <challber@villanova.edu> |
44 | * @author Thomas Wagener <wagener@hebis.uni-frankfurt.de> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org Main Page |
47 | */ |
48 | trait MergeRecursiveTrait |
49 | { |
50 | /** |
51 | * Determine if a variable is a string-keyed array |
52 | * |
53 | * @param mixed $op Variable to test |
54 | * |
55 | * @return boolean |
56 | */ |
57 | protected function isStringKeyedArray($op) |
58 | { |
59 | return is_array($op) && !array_is_list($op); |
60 | } |
61 | |
62 | /** |
63 | * Merge array recursive without combining single values to a new array |
64 | * as php's array_merge_recursive function does. |
65 | * |
66 | * @param array|string $val1 First Value |
67 | * @param array|string $val2 Second Value |
68 | * |
69 | * @return array|string merged values |
70 | */ |
71 | protected function mergeRecursive($val1, $val2) |
72 | { |
73 | if ($val2 === null || $val2 === []) { |
74 | return $val1; |
75 | } |
76 | |
77 | // Early escape for string, number, etc. values |
78 | if (!is_array($val2) && !is_array($val1)) { |
79 | return $val2; |
80 | } |
81 | |
82 | if (!$this->isStringKeyedArray($val2)) { |
83 | return array_merge((array)$val1, (array)$val2); |
84 | } |
85 | |
86 | if (!$this->isStringKeyedArray($val1)) { |
87 | // don't merge if incompatible |
88 | return $val2; |
89 | } |
90 | |
91 | foreach ($val1 as $key => $val) { |
92 | if (!array_key_exists($key, $val2)) { |
93 | // capture missing string keys |
94 | $val2[$key] = $val; |
95 | } else { |
96 | // recurse |
97 | $val2[$key] = $this->mergeRecursive($val, $val2[$key]); |
98 | } |
99 | } |
100 | return $val2; |
101 | } |
102 | } |