|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace FlmBus\Ini; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use FlmBus\Ini\Exceptions\InvalidDataException; |
|
8
|
|
|
|
|
9
|
|
|
class IniSection |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $name; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var IniSection|null |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $parent; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $contents = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Section constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param IniSection $parent |
|
29
|
|
|
*/ |
|
30
|
29 |
|
public function __construct($name, IniSection $parent = null) |
|
31
|
|
|
{ |
|
32
|
29 |
|
$this->name = $name; |
|
33
|
29 |
|
$this->parent = $parent; |
|
34
|
29 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param IniSection $parent |
|
38
|
|
|
* |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
10 |
|
public function setParent(IniSection $parent) |
|
42
|
|
|
{ |
|
43
|
10 |
|
$this->parent = $parent; |
|
44
|
|
|
|
|
45
|
10 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $data |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
* @throws InvalidDataException |
|
54
|
|
|
*/ |
|
55
|
23 |
|
public function setContents($data) |
|
56
|
|
|
{ |
|
57
|
23 |
|
if (!is_array($data)) |
|
58
|
23 |
|
{ |
|
59
|
1 |
|
throw new InvalidDataException('Invalid section contents! ' . |
|
60
|
1 |
|
'Section contents must be an array.'); |
|
61
|
|
|
} |
|
62
|
22 |
|
$this->contents = IniParser::i()->itemValuetoStringRepresentation($data); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
22 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
18 |
|
protected function getContents() |
|
71
|
|
|
{ |
|
72
|
18 |
|
return $this->contents; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
19 |
|
public function hasParent() |
|
79
|
|
|
{ |
|
80
|
19 |
|
return ($this->parent instanceof IniSection); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return IniSection|null |
|
85
|
|
|
*/ |
|
86
|
10 |
|
public function getParent() |
|
87
|
|
|
{ |
|
88
|
10 |
|
return $this->parent; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function getName() |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->name; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return IniSection[] |
|
102
|
|
|
*/ |
|
103
|
18 |
|
protected function getParents() |
|
104
|
|
|
{ |
|
105
|
18 |
|
$parents = []; |
|
106
|
18 |
|
$currentSection = $this; |
|
107
|
18 |
|
while ($currentSection->hasParent()) |
|
108
|
|
|
{ |
|
109
|
10 |
|
$parent = $currentSection->getParent(); |
|
110
|
10 |
|
$parents[] = $parent; |
|
111
|
10 |
|
$currentSection = $parent; |
|
112
|
10 |
|
} |
|
113
|
18 |
|
rsort($parents); |
|
114
|
|
|
|
|
115
|
18 |
|
return $parents; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
18 |
|
protected function composeContents() |
|
122
|
|
|
{ |
|
123
|
18 |
|
$contents = []; |
|
124
|
18 |
|
$parents = $this->getParents(); |
|
125
|
18 |
|
foreach ($parents as $section) |
|
126
|
|
|
{ |
|
127
|
10 |
|
$contents = array_merge($contents, $section->getContents()); |
|
128
|
18 |
|
} |
|
129
|
18 |
|
$contents = array_merge($contents, $this->getContents()); |
|
130
|
|
|
|
|
131
|
18 |
|
return $contents; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get normalized item value |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $itemName |
|
138
|
|
|
* @param mixed $defaultValue |
|
139
|
|
|
* |
|
140
|
|
|
* @return mixed |
|
141
|
|
|
*/ |
|
142
|
16 |
|
public function get($itemName, $defaultValue = null) |
|
143
|
|
|
{ |
|
144
|
16 |
|
$contents = $this->composeContents(); |
|
145
|
16 |
|
$value = isset($contents[$itemName]) ? $contents[$itemName] : $defaultValue; |
|
146
|
|
|
|
|
147
|
16 |
|
return IniParser::i()->castItemValueToProperType($value); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $itemName |
|
152
|
|
|
* @param string|array|bool|null $itemValue |
|
153
|
|
|
* |
|
154
|
|
|
* @return $this |
|
155
|
|
|
* @throws InvalidDataException |
|
156
|
|
|
*/ |
|
157
|
6 |
View Code Duplication |
public function set($itemName, $itemValue) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
6 |
|
list($validationResult, $message) = IniParser::i()->validateItemName($itemName); |
|
160
|
6 |
|
if (false === $validationResult) |
|
161
|
6 |
|
{ |
|
162
|
1 |
|
throw new InvalidDataException($message); |
|
163
|
|
|
} |
|
164
|
5 |
|
$this->contents[$itemName] = IniParser::i()->itemValuetoStringRepresentation($itemValue); |
|
165
|
|
|
|
|
166
|
5 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $itemName |
|
171
|
|
|
* |
|
172
|
|
|
* @return $this |
|
173
|
|
|
* @throws InvalidDataException |
|
174
|
|
|
*/ |
|
175
|
4 |
View Code Duplication |
public function delete($itemName) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
4 |
|
list($validationResult, $message) = IniParser::i()->validateItemName($itemName); |
|
178
|
4 |
|
if (false === $this->hasItem($itemName) || false === $validationResult) |
|
179
|
4 |
|
{ |
|
180
|
2 |
|
throw new InvalidDataException($message); |
|
181
|
|
|
} |
|
182
|
2 |
|
unset($this->contents[$itemName]); |
|
183
|
|
|
|
|
184
|
2 |
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param string $itemName |
|
189
|
|
|
* |
|
190
|
|
|
* @return bool |
|
191
|
|
|
*/ |
|
192
|
7 |
|
public function hasItem($itemName) |
|
193
|
|
|
{ |
|
194
|
7 |
|
$value = $this->get($itemName, null); |
|
195
|
|
|
|
|
196
|
7 |
|
return !is_null($value); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
2 |
|
public function toArray() |
|
203
|
|
|
{ |
|
204
|
2 |
|
return $this->composeContents(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function toString() |
|
211
|
|
|
{ |
|
212
|
1 |
|
$lines = $this->renderName(); |
|
213
|
1 |
|
foreach ($this->contents as $itemName => $itemValue) |
|
214
|
|
|
{ |
|
215
|
1 |
|
$lines = array_merge($lines, $this->renderItem($itemName, $itemValue)); |
|
216
|
1 |
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
return implode(PHP_EOL, $lines) . PHP_EOL; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return string[] |
|
223
|
|
|
*/ |
|
224
|
1 |
|
protected function renderName() |
|
225
|
|
|
{ |
|
226
|
|
|
|
|
227
|
1 |
|
if ($this->hasParent()) |
|
228
|
1 |
|
{ |
|
229
|
|
|
$line = [sprintf('[%s : %s]', $this->getName(), $this->getParent()->getName())]; |
|
230
|
|
|
} |
|
231
|
|
|
else |
|
232
|
|
|
{ |
|
233
|
1 |
|
$line = [sprintf('[%s]', $this->getName())]; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
1 |
|
return $line; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param string $name |
|
241
|
|
|
* @param string|array $value |
|
242
|
|
|
* |
|
243
|
|
|
* @return array |
|
244
|
|
|
*/ |
|
245
|
1 |
|
protected function renderItem($name, $value) |
|
246
|
|
|
{ |
|
247
|
1 |
|
if (is_array($value)) |
|
248
|
1 |
|
{ |
|
249
|
|
|
$lines = $this->renderArrayItem($name, $value); |
|
250
|
|
|
} |
|
251
|
|
|
else |
|
252
|
|
|
{ |
|
253
|
1 |
|
$lines = $this->renderStringItem($name, $value); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
1 |
|
return $lines; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @param string $name |
|
261
|
|
|
* @param string $value |
|
262
|
|
|
* |
|
263
|
|
|
* @return string[] |
|
264
|
|
|
*/ |
|
265
|
1 |
|
protected function renderStringItem($name, $value) |
|
266
|
|
|
{ |
|
267
|
1 |
|
return [sprintf('%s = "%s"', $name, $value)]; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param string $name |
|
272
|
|
|
* @param array $values |
|
273
|
|
|
* |
|
274
|
|
|
* @return array |
|
275
|
|
|
*/ |
|
276
|
|
|
protected function renderArrayItem($name, array $values) |
|
277
|
|
|
{ |
|
278
|
|
|
$lines = []; |
|
279
|
|
|
$isAssocArray = (array_values($values) !== $values); |
|
280
|
|
|
foreach ($values as $key => $value) |
|
281
|
|
|
{ |
|
282
|
|
|
$stringKey = $isAssocArray ? $key : ''; |
|
283
|
|
|
$lines[] = sprintf('%s[%s] = "%s"', $name, $stringKey, $value); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $lines; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.