|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BinaryCube\CarrotMQ; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Config |
|
7
|
|
|
* |
|
8
|
|
|
* @package BinaryCube\CarrotMQ |
|
9
|
|
|
*/ |
|
10
|
|
|
class Config |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $config = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $config |
|
20
|
|
|
* |
|
21
|
|
|
* @return static |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function create(array $config = []) |
|
24
|
|
|
{ |
|
25
|
|
|
return (new static($config)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $config |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(array $config = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->config = $config; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function set(array $config) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->config = $config; |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function get() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->toArray(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* If each array has an element with the same string key value, the latter |
|
60
|
|
|
* will overwrite the former (different from array_merge_recursive). |
|
61
|
|
|
* Recursive merging will be conducted if both arrays have an element of array |
|
62
|
|
|
* type and are having the same key. |
|
63
|
|
|
* For integer-keyed elements, the elements from the latter array will |
|
64
|
|
|
* be appended to the former array. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $a array to be merged from. You can specify additional |
|
67
|
|
|
* arrays via second argument, third argument, fourth argument etc. |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
|
|
public function mergeWith($a) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->config = $this->merge($this->config, $a); |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $a |
|
80
|
|
|
* @param array $b |
|
81
|
|
|
* |
|
82
|
|
|
* @return array|mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
protected static function merge($a, $b) |
|
85
|
|
|
{ |
|
86
|
|
|
$args = \func_get_args(); |
|
87
|
|
|
$res = \array_shift($args); |
|
88
|
|
|
|
|
89
|
|
|
while (!empty($args)) { |
|
90
|
|
|
foreach (\array_shift($args) as $k => $v) { |
|
91
|
|
|
if (\is_int($k)) { |
|
92
|
|
|
if (\array_key_exists($k, $res)) { |
|
93
|
|
|
$res[] = $v; |
|
94
|
|
|
} else { |
|
95
|
|
|
$res[$k] = $v; |
|
96
|
|
|
} |
|
97
|
|
|
} elseif (\is_array($v) && isset($res[$k]) && \is_array($res[$k])) { |
|
98
|
|
|
$res[$k] = static::merge($res[$k], $v); |
|
99
|
|
|
} else { |
|
100
|
|
|
$res[$k] = $v; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $res; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function toArray() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->config; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|