|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use IteratorAggregate; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Config |
|
10
|
|
|
* |
|
11
|
|
|
* Manage multidimensional array. Config allows developer to easy get a value or a node |
|
|
|
|
|
|
12
|
|
|
* |
|
13
|
|
|
* <code> |
|
14
|
|
|
* $config = new Config([ |
|
15
|
|
|
* 'connection_name' => ['item1' => 'value1'] |
|
16
|
|
|
* ]); |
|
17
|
|
|
* $config->get('connection_name'); //['item1' => 'value1'] |
|
18
|
|
|
* </code> |
|
19
|
|
|
* |
|
20
|
|
|
* @deprecated Since 1.1 |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
class ConnectionConfig implements IteratorAggregate, ArrayAccess |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $config; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set the config content and the read only mode |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $config Data |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $config = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->config = $config; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function get($key, $default = null) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->raw($key, $default); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
|
|
|
|
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function has($key) |
|
51
|
|
|
{ |
|
52
|
|
|
return isset($this->config[$key]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set datum |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $key |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function set($key, $value) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($value instanceof self) { |
|
66
|
|
|
$value = $value->config; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($key === null) { |
|
|
|
|
|
|
70
|
|
|
$this->config[] = $value; |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->config[$key] = $value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Remove a datum |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $key |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function remove($key) |
|
86
|
|
|
{ |
|
87
|
|
|
unset($this->config[$key]); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function all() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->config; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* SPL - IteratorAggregate |
|
102
|
|
|
* |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getIterator() |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($this->config as $key => $value) { |
|
108
|
|
|
yield $key => $this->get($key); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
|
|
|
|
|
113
|
|
|
* SPL - ArrayAccess |
|
114
|
|
|
* |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function offsetExists($offset) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->has($offset); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
|
|
|
|
|
123
|
|
|
* SPL - ArrayAccess |
|
124
|
|
|
* |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function offsetGet($offset) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->get($offset); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
|
|
|
|
|
133
|
|
|
* SPL - ArrayAccess |
|
134
|
|
|
* |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function offsetSet($offset, $value) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->set($offset, $value); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
|
|
|
|
|
143
|
|
|
* SPL - ArrayAccess |
|
144
|
|
|
* |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
|
|
public function offsetUnset($offset) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->remove($offset); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the raw value |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $key |
|
156
|
|
|
* @param mixed $default |
|
157
|
|
|
* |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
private function raw($key, $default = null) |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
return $this->config[$key] ?? $default; |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|