|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Yosymfony\Config-loader. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) YoSymfony <http://github.com/yosymfony> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Yosymfony\ConfigLoader; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Simple implementation of a configuration repository. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Victor Puertas <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Repository implements RepositoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
protected $repository = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Construct a configuration repository from an array |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $values |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(array $values = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->repository = $values; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get(string $key, $default = null) |
|
37
|
|
|
{ |
|
38
|
|
|
return isset($this->repository[$key]) ? $this->repository[$key] : $default; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function set(string $key, $value) : void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->offsetSet($key, $value); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function del(string $key) : void |
|
53
|
|
|
{ |
|
54
|
|
|
if (array_key_exists($key, $this->repository)) { |
|
55
|
|
|
unset($this->repository[$key]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function union(RepositoryInterface $repository) : RepositoryInterface |
|
63
|
|
|
{ |
|
64
|
|
|
$union = function (array $r1, array $r2) use (&$union) { |
|
65
|
|
|
$res = $r1; |
|
66
|
|
|
foreach ($r2 as $k => $v) { |
|
67
|
|
|
if (isset($res[$k]) && is_array($r1[$k])) { |
|
68
|
|
|
$res[$k] = $union($r1[$k], $v); |
|
69
|
|
|
} elseif (!isset($res[$k])) { |
|
70
|
|
|
$res[$k] = $v; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $res; |
|
75
|
|
|
}; |
|
76
|
|
|
|
|
77
|
|
|
return new self($union($this->getArray(), $repository->getArray())); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function intersection(RepositoryInterface $repository) : RepositoryInterface |
|
84
|
|
|
{ |
|
85
|
|
|
$interception = function ($main, $second) { |
|
86
|
|
|
$result = new Repository(); |
|
87
|
|
|
$keysMain = array_keys($main->getArray()); |
|
88
|
|
|
$keysSecond = array_keys($second->getArray()); |
|
89
|
|
|
$keys = array_intersect($keysMain, $keysSecond); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($keys as $key) { |
|
92
|
|
|
$result[$key] = $main[$key]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $result; |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
return $interception($this, $repository); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc}. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getArray() : array |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->repository; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set a new key (From ArrayAccess interface). |
|
111
|
|
|
*/ |
|
112
|
|
|
public function offsetSet($offset, $value) |
|
113
|
|
|
{ |
|
114
|
|
|
if (is_null($offset)) { |
|
115
|
|
|
$this->repository[] = $value; |
|
116
|
|
|
} else { |
|
117
|
|
|
$this->repository[$offset] = $value; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Check if a key exists (from ArrayAccess interface). |
|
123
|
|
|
*/ |
|
124
|
|
|
public function offsetExists($offset) |
|
125
|
|
|
{ |
|
126
|
|
|
return isset($this->repository[$offset]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Delete a key (from ArrayAccess interface). |
|
131
|
|
|
*/ |
|
132
|
|
|
public function offsetUnset($offset) |
|
133
|
|
|
{ |
|
134
|
|
|
unset($this->repository[$offset]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Retrueve a key (from ArrayAccess interface). |
|
139
|
|
|
*/ |
|
140
|
|
|
public function offsetGet($offset) |
|
141
|
|
|
{ |
|
142
|
|
|
return isset($this->repository[$offset]) ? $this->repository[$offset] : null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Count of element of a repository (from Countable interface). |
|
147
|
|
|
*/ |
|
148
|
|
|
public function count() |
|
149
|
|
|
{ |
|
150
|
|
|
return count($this->repository); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set the pointer to the first element (from Iterator interface). |
|
155
|
|
|
*/ |
|
156
|
|
|
public function rewind() |
|
157
|
|
|
{ |
|
158
|
|
|
return reset($this->repository); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get the current element (from Iterator interface). |
|
163
|
|
|
*/ |
|
164
|
|
|
public function current() |
|
165
|
|
|
{ |
|
166
|
|
|
return current($this->repository); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the current position (from Iterator interface). |
|
171
|
|
|
*/ |
|
172
|
|
|
public function key() |
|
173
|
|
|
{ |
|
174
|
|
|
return key($this->repository); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Set the pointer to the next element (from Iterator interface). |
|
179
|
|
|
*/ |
|
180
|
|
|
public function next() |
|
181
|
|
|
{ |
|
182
|
|
|
return next($this->repository); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Checks if the current position is valid (from Iterator interface). |
|
187
|
|
|
*/ |
|
188
|
|
|
public function valid() |
|
189
|
|
|
{ |
|
190
|
|
|
return key($this->repository) !== null; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|