1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Logikos\Util; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This is largely inspired by \Phalcon\Config - https://docs.phalconphp.com/hr/3.2/api/Phalcon_Config |
7
|
|
|
* NOTICE: \Phalcon\Config will be much faster than this class and you are encouraged to use it |
8
|
|
|
* @see ../docs/config/README.md |
9
|
|
|
*/ |
10
|
|
|
abstract class Config implements \ArrayAccess, \Countable, \Iterator { |
11
|
|
|
private $locked = false; |
12
|
|
|
private $values = []; |
13
|
|
|
|
14
|
|
|
public function __construct(array $arrayConfig = []) { |
15
|
|
|
foreach($arrayConfig as $key => $value) |
16
|
|
|
$this->offsetSet($key, $value); |
17
|
|
|
|
18
|
|
|
$this->onConstruct(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// override this if you want to |
22
|
|
|
protected function onConstruct() {} |
23
|
|
|
|
24
|
|
|
public function isLocked() { |
25
|
|
|
return $this->locked; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function get($key, $default = null) { |
29
|
|
|
return $this->offsetExists($key) ? $this->offsetGet($key) : $default; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function has($key) { |
33
|
|
|
return $this->offsetExists($key); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function toArray() { |
37
|
|
|
$array = []; |
38
|
|
|
foreach ($this->values as $key => $value) { |
39
|
|
|
if (is_object($value) && method_exists($value, 'toArray')) { |
40
|
|
|
$array[$key] = $value->toArray(); |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
$array[$key] = $value; |
44
|
|
|
} |
45
|
|
|
return $array; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function path($path, $default = null, $delimiter = '.') { |
49
|
|
|
$tokens = explode($delimiter, $path); |
50
|
|
|
$token = array_shift($tokens); |
51
|
|
|
if ($this->get($token) instanceof self) |
52
|
|
|
return $this[$token]->path(implode('.', $tokens), $default); |
53
|
|
|
return $this->get($token, $default); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
# Countable |
58
|
|
|
public function count() { |
59
|
|
|
return count($this->values); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
# ArrayAccess |
64
|
|
|
public function offsetExists($offset) { |
65
|
|
|
return array_key_exists($offset, $this->values); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function offsetGet($offset) { |
69
|
|
|
if (!$this->offsetExists($offset)) |
70
|
|
|
throw new \OutOfBoundsException("offset '{$offset}' does not exist"); |
71
|
|
|
return $this->values[$offset]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function offsetSet($offset, $value) { |
75
|
|
|
$this->blockIfLocked(); |
76
|
|
|
$this->values[strval($offset)] = is_array($value) |
77
|
|
|
? new static($value) |
78
|
|
|
: $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function offsetUnset($offset) { |
82
|
|
|
$this->blockIfLocked(); |
83
|
|
|
unset($this->values[strval($offset)]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
# Iterator |
88
|
|
|
public function rewind() { return reset($this->values); } |
89
|
|
|
public function key() { return key($this->values); } |
90
|
|
|
public function current() { return current($this->values); } |
91
|
|
|
public function next() { return next($this->values); } |
92
|
|
|
public function valid() { return key($this->values) !== null; } |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
# Magic Property Access |
96
|
|
|
public function __set($offset, $value) { $this->offsetSet($offset, $value); } |
97
|
|
|
public function __unset($offset) { $this->offsetUnset($offset); } |
98
|
|
|
public function __get($offset) { return $this->offsetGet($offset); } |
99
|
|
|
public function __isset($offset) { return $this->offsetExists($offset); } |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $data |
103
|
|
|
* @return static |
104
|
|
|
*/ |
105
|
|
|
public static function __set_state(array $data) { |
106
|
|
|
return new static($data); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function lock() { |
110
|
|
|
$this->locked = true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function rawValues() { |
114
|
|
|
return $this->values; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function isConfigObject($value) { |
118
|
|
|
return $value instanceof Config; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function blockIfLocked() { |
122
|
|
|
if ($this->isLocked()) |
|
|
|
|
123
|
|
|
throw new CanNotMutateException(); |
124
|
|
|
} |
125
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.