forked from overtrue/socialite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
95 lines (72 loc) · 2.35 KB
/
Config.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
<?php
namespace Overtrue\Socialite;
use ArrayAccess;
use JsonSerializable;
class Config implements ArrayAccess, JsonSerializable
{
protected array $config;
public function __construct(array $config)
{
$this->config = $config;
}
public function get(string $key, mixed $default = null): mixed
{
$config = $this->config;
if (isset($config[$key])) {
return $config[$key];
}
foreach (\explode('.', $key) as $segment) {
if (! \is_array($config) || ! \array_key_exists($segment, $config)) {
return $default;
}
$config = $config[$segment];
}
return $config;
}
public function set(string $key, mixed $value): array
{
$keys = \explode('.', $key);
$config = &$this->config;
while (\count($keys) > 1) {
$key = \array_shift($keys);
if (! isset($config[$key]) || ! \is_array($config[$key])) {
$config[$key] = [];
}
$config = &$config[$key];
}
$config[\array_shift($keys)] = $value;
return $config;
}
public function has(string $key): bool
{
return (bool) $this->get($key);
}
public function offsetExists(mixed $offset): bool
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');
return \array_key_exists($offset, $this->config);
}
public function offsetGet(mixed $offset): mixed
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');
return $this->get($offset);
}
public function offsetSet(mixed $offset, mixed $value): void
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');
$this->set($offset, $value);
}
public function offsetUnset(mixed $offset): void
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');
$this->set($offset, null);
}
public function jsonSerialize(): array
{
return $this->config;
}
public function __toString(): string
{
return \json_encode($this, \JSON_UNESCAPED_UNICODE) ?: '';
}
}