1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace micmania1\config\MergeStrategy; |
4
|
|
|
|
5
|
|
|
use micmania1\config\ConfigCollectionInterface; |
6
|
|
|
use micmania1\config\ConfigCollection; |
7
|
|
|
|
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) |
49
|
|
|
{ |
50
|
2 |
|
foreach($highPriority as $key => $value) { |
51
|
|
|
// If value isn't an array, we can overwrite whatever was before it |
52
|
2 |
|
if(!is_array($value)) { |
53
|
2 |
|
if(is_int($key)) { |
54
|
1 |
|
$lowPriority[] = $value; |
55
|
1 |
|
} else { |
56
|
1 |
|
$lowPriority[$key] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If not set, or we're changing type we can set low priority |
63
|
|
|
if(!isset($lowPriority[$key]) || !is_array($lowPriority[$key])) { |
64
|
|
|
if(is_int($key)) { |
65
|
|
|
$lowPriority[] = $value; |
66
|
|
|
} else { |
67
|
|
|
$lowPriority[$key] = $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// We have two arrays, so we merge |
74
|
|
|
$lowPriority[$key] = $this->mergeArray($value, $lowPriority[$key]); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
2 |
|
return $lowPriority; |
78
|
|
|
} |
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) |
88
|
|
|
{ |
89
|
16 |
|
if(!isset($item['value'])) { |
90
|
|
|
$item['value'] = ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
16 |
|
if(!isset($item['metadata'])) { |
94
|
5 |
|
$item['metadata'] = []; |
95
|
5 |
|
} |
96
|
|
|
|
97
|
16 |
|
return ['value' => $item['value'], 'metadata' => $item['metadata']]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|