1 | <?php |
||
8 | class Priority |
||
9 | { |
||
10 | 16 | public function merge(array $mine, ConfigCollectionInterface $theirs) { |
|
11 | 16 | foreach ($mine as $key => $item) { |
|
12 | |||
13 | // Ensure we have value/metadata keys |
||
14 | 16 | $item = $this->normaliseItem($item); |
|
15 | |||
16 | // If the item doesn't exist in theirs, we can just set it and continue. |
||
17 | 16 | $theirValue = $theirs->get($key); |
|
18 | 16 | if(is_null($theirValue)) { |
|
19 | 11 | $theirs->set($key, $item['value']); |
|
20 | 11 | continue; |
|
21 | } |
||
22 | |||
23 | // Get the two values for comparison |
||
24 | 11 | $value = $item['value']; |
|
25 | |||
26 | // If its an array and the key already esists, we can use array_merge |
||
27 | 11 | if (is_array($value) && is_array($theirValue)) { |
|
28 | 2 | $value = $this->mergeArray($value, $theirValue); |
|
29 | 2 | } |
|
30 | |||
31 | // The key is not set or the value is to be overwritten |
||
32 | 11 | $theirs->set($key, $value, $item['metadata']); |
|
33 | 16 | } |
|
34 | |||
35 | 16 | return $theirs; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Deep merges a high priorty array into a lower priority array, overwriting duplicate |
||
40 | * keys. If the keys are integers, then the merges acts like array_merge() and adds a new |
||
41 | * item. |
||
42 | * |
||
43 | * @param array $highPriority |
||
44 | * @param array $lowPriority |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | 2 | public function mergeArray(array $highPriority, array $lowPriority) |
|
79 | |||
80 | /** |
||
81 | * Returns a normalised array with value/metadata keys |
||
82 | * |
||
83 | * @param array |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | 16 | protected function normaliseItem(array $item) |
|
99 | } |
||
100 |