1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\ArrayMerger; |
4
|
|
|
|
5
|
|
|
use Graze\ArrayMerger\ValueMerger\LastValue; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ArrayMerger |
9
|
|
|
* |
10
|
|
|
* This does a simple, `array_merge`, but you can choose how to merge the values yourself |
11
|
|
|
*/ |
12
|
|
|
class ArrayMerger implements ArrayMergerInterface |
13
|
|
|
{ |
14
|
|
|
use StaticMethodsTrait; |
15
|
|
|
|
16
|
|
|
/** @var callable */ |
17
|
|
|
protected $valueMerger; |
18
|
|
|
/** @var int */ |
19
|
|
|
private $flags; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param callable $valueMerger |
23
|
|
|
* @param int $flags |
24
|
|
|
*/ |
25
|
|
|
public function __construct(callable $valueMerger = null, $flags = 0) |
26
|
|
|
{ |
27
|
|
|
$this->valueMerger = $valueMerger ?: new LastValue(); |
28
|
|
|
$this->flags = $flags; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Merge using the supplied Value Merge |
33
|
|
|
* |
34
|
|
|
* @param callable $valueMerger |
35
|
|
|
* @param array $array1 |
36
|
|
|
* @param array $arrays |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function mergeUsing(callable $valueMerger, array $array1, array $arrays) |
41
|
|
|
{ |
42
|
|
|
$merger = new static($valueMerger); |
43
|
|
|
return call_user_func_array([$merger, 'merge'], array_merge([$array1], array_slice(func_get_args(), 2))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Merge the values from all the array supplied, the first array is treated as the base array to merge into |
48
|
|
|
* |
49
|
|
|
* @param array $array1 |
50
|
|
|
* @param array|null $arrays |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function merge(array $array1, array $arrays = null) |
55
|
|
|
{ |
56
|
|
|
$arrays = array_slice(func_get_args(), 1); |
57
|
|
|
if (count($arrays) === 0) { |
58
|
|
|
return $array1; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// if all arrays are sequential and flag is set, append them all |
62
|
|
|
if ($this->flags & static::FLAG_APPEND_VALUE_ARRAY == static::FLAG_APPEND_VALUE_ARRAY |
63
|
|
|
&& array_is_sequential($array1) |
64
|
|
|
&& count(array_filter($arrays, 'Graze\ArrayMerger\array_is_sequential')) === count($arrays)) { |
65
|
|
|
return call_user_func_array('array_merge', array_merge([$array1], $arrays)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$merged = $array1; |
69
|
|
|
|
70
|
|
|
foreach ($arrays as $toMerge) { |
71
|
|
|
foreach ($toMerge as $key => &$value) { |
72
|
|
|
if (array_key_exists($key, $merged)) { |
73
|
|
|
if ($this->flags & static::FLAG_APPEND_VALUE_ARRAY == static::FLAG_APPEND_VALUE_ARRAY |
74
|
|
|
&& is_array($value) |
75
|
|
|
&& is_array($merged[$key]) |
76
|
|
|
&& array_is_sequential($value) |
77
|
|
|
&& array_is_sequential($merged[$key])) { |
78
|
|
|
$merged[$key] = array_merge($merged[$key], $value); |
79
|
|
|
} else { |
80
|
|
|
$merged[$key] = call_user_func($this->valueMerger, $merged[$key], $value); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
$merged[$key] = $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $merged; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|