-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.php
173 lines (151 loc) · 3.71 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Qubus\Config
*
* @link https://github.com/QubusPHP/config
* @copyright 2020 Joshua Parker <[email protected]>
* @copyright 2016 Sinergi
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Config;
use ArrayAccess;
use Qubus\Exception\Data\TypeException;
use function array_merge;
use function is_string;
use function sprintf;
class Collection extends Configuration implements ArrayAccess, ConfigContainer
{
/** @var array $container */
private array $container = [];
/**
* @param array|Configuration $config
*/
public static function factory($config): Collection
{
$factory = new Factory();
return $factory($config);
}
/**
* Set a config
*
* @param string $key
* @param mixed $value
* @return self
*/
public function setConfigKey(string $key, mixed $value): static
{
if (! isset($this->container[$key])) {
$this->container[$key] = $value;
} else {
$this->container[$key] = array_merge($this->container[$key], $value);
}
return $this;
}
/**
* Checks if a key exists.
*/
public function hasConfigKey(string $key): bool
{
return isset($this->container[$key]);
}
public function removeConfigKey(string $key): void
{
unset($this->container[$key]);
}
/**
* Get a config
*
* @param string $key
* @param mixed|null $default
* @return mixed
* @throws TypeException
*/
public function getConfigKey(string $key, mixed $default = null): mixed
{
if (! is_string($key) || empty($key)) {
throw new TypeException(
sprintf('Parameter %s passed to Config::get() is not a valid string resource.', $key),
);
}
[$file, $configKey, $sub] = Parser::getKey($key);
if (! isset($this->container[$file])) {
$this->container[$file] = ConfigLoader::load(
$this->paths,
$this->environment,
$file
);
}
return Parser::getValue($this->container[$file], $configKey, $sub, $default);
}
/**
* @return $this
*/
public function reset(): static
{
$this->container = [];
return $this;
}
/**
* @param string $key
* @return mixed
* @throws TypeException
*/
public function __get($key)
{
return $this->offsetGet($key);
}
/**
* @param mixed $key
* @param array|null $args
* @return mixed
* @throws TypeException
*/
public function __call(mixed $key, array $args = null)
{
return $this->offsetGet($key);
}
/**
* @param mixed $key
* @return bool
* @throws TypeException
*/
public function __isset(mixed $key)
{
return $this->offsetExists($key);
}
/**
* @param mixed $offset
* @return bool
* @throws TypeException
*/
public function offsetExists(mixed $offset): bool
{
$item = $this->getConfigKey($offset);
return isset($item);
}
/**
* @param mixed $offset
* @return mixed
* @throws TypeException
*/
public function offsetGet(mixed $offset): mixed
{
return $this->getConfigKey($offset);
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->setConfigKey($offset, $value);
}
/**
* @param mixed $offset
*/
public function offsetUnset(mixed $offset): void
{
$this->removeConfigKey($offset);
}
}