1 | <?php declare(strict_types=1); |
||
15 | class Configuration implements \ArrayAccess |
||
16 | { |
||
17 | public $parent; |
||
18 | private $collection; |
||
19 | |||
20 | 33 | public function __construct(Configuration $parent = null) |
|
21 | { |
||
22 | 33 | $this->parent = $parent; |
|
23 | 33 | $this->collection = new Collection(); |
|
24 | 33 | } |
|
25 | |||
26 | public function getCollection(): Collection |
||
30 | |||
31 | public function setCollection(Collection $collection) |
||
32 | { |
||
33 | $this->collection = $collection; |
||
34 | } |
||
35 | |||
36 | 33 | public function set(string $name, $value) |
|
37 | { |
||
38 | 33 | $this->collection[$name] = $value; |
|
39 | 33 | } |
|
40 | |||
41 | 5 | public function add(string $name, array $array) |
|
42 | { |
||
43 | 5 | if ($this->has($name)) { |
|
44 | 5 | $config = $this->get($name); |
|
45 | 5 | if (!is_array($config)) { |
|
46 | 2 | throw new ConfigurationException("Configuration parameter `$name` isn't array."); |
|
47 | } |
||
48 | 3 | $this->set($name, array_merge_alternate($config, $array)); |
|
49 | } else { |
||
50 | $this->set($name, $array); |
||
51 | } |
||
52 | 3 | } |
|
53 | |||
54 | 23 | public function get(string $name, $default = null) |
|
74 | |||
75 | 8 | public function has(string $name): bool |
|
76 | { |
||
77 | 8 | return $this->collection->has($name); |
|
78 | } |
||
79 | |||
80 | 23 | public function parse($value) |
|
81 | { |
||
82 | 23 | if (is_string($value)) { |
|
83 | 18 | return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', [$this, 'parseCallback'], $value); |
|
84 | } |
||
85 | |||
86 | 14 | return $value; |
|
87 | } |
||
88 | |||
89 | 1 | public function offsetExists($offset) |
|
90 | { |
||
91 | 1 | return $this->has($offset); |
|
92 | } |
||
93 | |||
94 | 3 | public function offsetGet($offset) |
|
98 | |||
99 | 10 | public function offsetSet($offset, $value) |
|
100 | { |
||
101 | 10 | $this->set($offset, $value); |
|
102 | 10 | } |
|
103 | |||
104 | public function offsetUnset($offset) |
||
105 | { |
||
106 | unset($this->collection[$offset]); |
||
108 | |||
109 | public function persist() |
||
123 | |||
124 | 3 | private function parseCallback(array $matches) |
|
128 | |||
129 | 22 | private function isClosure($var) |
|
133 | } |
||
134 |