|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Retrinko\Ini; |
|
5
|
|
|
|
|
6
|
|
|
use Retrinko\Ini\Exceptions\InvalidDataException; |
|
7
|
|
|
|
|
8
|
|
|
class IniParser |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
const SECTION_INHERITANCE_OPERATOR = ':'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var IniParser |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $instance; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $invalidCharsForItemNames = '?{}|&~!()^"'; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $invalidItemNames = ['null', 'yes', 'no', 'true', 'false', 'on', 'off', 'none']; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* IniParser constructor. |
|
28
|
|
|
*/ |
|
29
|
1 |
|
protected function __construct() |
|
30
|
|
|
{ |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return IniParser |
|
35
|
|
|
*/ |
|
36
|
36 |
|
public static function i() |
|
37
|
|
|
{ |
|
38
|
36 |
|
return static::getInstance(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return IniParser |
|
43
|
|
|
*/ |
|
44
|
36 |
|
public static function getInstance() |
|
45
|
|
|
{ |
|
46
|
36 |
|
if (is_null(static::$instance)) |
|
47
|
36 |
|
{ |
|
48
|
1 |
|
static::$instance = new static(); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
36 |
|
return static::$instance; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function __clone() |
|
55
|
|
|
{ |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
|
59
|
|
|
private function __wakeup() |
|
60
|
|
|
{ |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $string |
|
65
|
|
|
* |
|
66
|
|
|
* @return IniSection[] |
|
67
|
|
|
* @throws InvalidDataException |
|
68
|
|
|
*/ |
|
69
|
18 |
|
public function parseIniString($string) |
|
70
|
|
|
{ |
|
71
|
18 |
|
$parsedContents = []; |
|
72
|
|
|
|
|
73
|
18 |
|
if (strlen($string) > 0) |
|
74
|
18 |
|
{ |
|
75
|
11 |
|
$rawContents = @parse_ini_string($string, true, INI_SCANNER_RAW); |
|
76
|
11 |
|
if (false === $rawContents) |
|
77
|
11 |
|
{ |
|
78
|
1 |
|
throw new InvalidDataException('Error parsing ini string!'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
10 |
|
$parsedContents = $this->parseArray($rawContents); |
|
82
|
10 |
|
} |
|
83
|
|
|
|
|
84
|
17 |
|
return $parsedContents; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Cast item string value to proper type |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $value |
|
91
|
|
|
* |
|
92
|
|
|
* @return array|string|bool|int|float|null |
|
93
|
|
|
*/ |
|
94
|
13 |
|
public function castItemValueToProperType($value) |
|
95
|
|
|
{ |
|
96
|
13 |
|
$normalized = $value; |
|
97
|
|
|
|
|
98
|
13 |
|
if (in_array($value, ['true', 'on', 'yes'])) |
|
99
|
13 |
|
{ |
|
100
|
2 |
|
$normalized = true; |
|
101
|
2 |
|
} |
|
102
|
13 |
|
elseif (in_array($value, ['false', 'off', 'no', 'none'])) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$normalized = false; |
|
105
|
1 |
|
} |
|
106
|
12 |
|
elseif ('null' == $value) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$normalized = null; |
|
109
|
1 |
|
} |
|
110
|
12 |
|
elseif (is_numeric($value)) |
|
111
|
|
|
{ |
|
112
|
3 |
|
$number = $value + 0; |
|
113
|
3 |
|
if (intval($number) == $number) |
|
114
|
3 |
|
{ |
|
115
|
2 |
|
$normalized = (int)$number; |
|
116
|
2 |
|
} |
|
117
|
2 |
|
elseif (floatval($number) == $number) |
|
118
|
|
|
{ |
|
119
|
2 |
|
$normalized = (float)$number; |
|
120
|
2 |
|
} |
|
121
|
3 |
|
} |
|
122
|
|
|
|
|
123
|
13 |
|
return $normalized; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get an string (or an array of strings) representation of $value |
|
128
|
|
|
* |
|
129
|
|
|
* @param bool|int|float|null|array $value |
|
130
|
|
|
* |
|
131
|
|
|
* @return array|string |
|
132
|
|
|
* @throws InvalidDataException |
|
133
|
|
|
*/ |
|
134
|
22 |
|
public function itemValuetoStringRepresentation($value) |
|
135
|
|
|
{ |
|
136
|
22 |
|
if (is_bool($value)) |
|
137
|
22 |
|
{ |
|
138
|
2 |
|
$castedValue = (true === $value) ? 'true' : 'false'; |
|
139
|
2 |
|
} |
|
140
|
22 |
|
elseif (is_null($value)) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$castedValue = 'null'; |
|
143
|
2 |
|
} |
|
144
|
22 |
|
elseif (is_array($value)) |
|
145
|
|
|
{ |
|
146
|
19 |
|
$castedValue = []; |
|
147
|
19 |
|
foreach ($value as $k => $v) |
|
148
|
|
|
{ |
|
149
|
19 |
|
$castedValue[$k] = $this->itemValuetoStringRepresentation($v); |
|
150
|
19 |
|
} |
|
151
|
19 |
|
} |
|
152
|
22 |
|
elseif (is_numeric($value)) |
|
153
|
|
|
{ |
|
154
|
12 |
|
$castedValue = (string)$value; |
|
155
|
12 |
|
} |
|
156
|
22 |
|
elseif (is_string($value)) |
|
157
|
|
|
{ |
|
158
|
22 |
|
$castedValue = $value; |
|
159
|
22 |
|
} |
|
160
|
|
|
else |
|
161
|
|
|
{ |
|
162
|
|
|
throw new InvalidDataException('Invalid item value type!'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
22 |
|
return $castedValue; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $name |
|
170
|
|
|
* |
|
171
|
|
|
* @return array [validationResult, message] |
|
172
|
|
|
*/ |
|
173
|
5 |
|
public function validateItemName($name) |
|
174
|
|
|
{ |
|
175
|
5 |
|
$valid = true; |
|
176
|
5 |
|
$message = 'Valid item name.'; |
|
177
|
5 |
|
if (!is_string($name)) |
|
178
|
5 |
|
{ |
|
179
|
|
|
$valid = false; |
|
180
|
|
|
$message = 'Only string values are allowed for item names!'; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
5 |
|
if (in_array($name, $this->invalidItemNames)) |
|
184
|
5 |
|
{ |
|
185
|
1 |
|
$valid = false; |
|
186
|
1 |
|
$message = sprintf('Item name is not allowed! Not allowed item names: %s', |
|
187
|
1 |
|
implode(', ', $this->invalidItemNames)); |
|
188
|
1 |
|
} |
|
189
|
|
|
|
|
190
|
5 |
|
if (preg_match(sprintf('/[%s]/', $this->invalidCharsForItemNames), $name) > 0) |
|
191
|
5 |
|
{ |
|
192
|
2 |
|
$valid = false; |
|
193
|
2 |
|
$message = sprintf('Invalid name for ini item! Provided item name contains not ' . |
|
194
|
2 |
|
'allowed chars (%s).', $this->invalidCharsForItemNames); |
|
195
|
2 |
|
} |
|
196
|
|
|
|
|
197
|
5 |
|
return [$valid, $message]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param array $rawContents |
|
203
|
|
|
* |
|
204
|
|
|
* @return IniSection[] |
|
205
|
|
|
* @throws InvalidDataException |
|
206
|
|
|
*/ |
|
207
|
15 |
|
public function parseArray(array $rawContents) |
|
208
|
|
|
{ |
|
209
|
15 |
|
$parsedContents = []; |
|
210
|
15 |
|
foreach ($rawContents as $sectionFullName => $sectionContents) |
|
211
|
|
|
{ |
|
212
|
13 |
|
$pieces = explode(self::SECTION_INHERITANCE_OPERATOR, $sectionFullName, 2); |
|
213
|
13 |
|
$sectionName = trim($pieces[0]); |
|
214
|
13 |
|
if (!is_array($sectionContents)) |
|
215
|
13 |
|
{ |
|
216
|
2 |
|
throw new InvalidDataException(sprintf('Orphan fields are not allowed! ' . |
|
217
|
2 |
|
'Please define a section for field "%s".', |
|
218
|
2 |
|
$sectionName)); |
|
219
|
|
|
} |
|
220
|
11 |
|
$parsedContents[$sectionName] = new IniSection($sectionName); |
|
221
|
11 |
|
$parsedContents[$sectionName]->setContents($sectionContents); |
|
222
|
11 |
|
$parentName = isset($pieces[1]) ? trim($pieces[1]) : null; |
|
223
|
11 |
|
if (!is_null($parentName)) |
|
224
|
11 |
|
{ |
|
225
|
10 |
|
if (!isset($parsedContents[$parentName])) |
|
226
|
10 |
|
{ |
|
227
|
|
|
throw new InvalidDataException(sprintf('Parent section not found! ' . |
|
228
|
|
|
'Define "%s" section before "%s" section.', |
|
229
|
|
|
$parentName, $sectionName)); |
|
230
|
|
|
} |
|
231
|
10 |
|
$parsedContents[$sectionName]->setParent($parsedContents[$parentName]); |
|
232
|
10 |
|
} |
|
233
|
13 |
|
} |
|
234
|
|
|
|
|
235
|
13 |
|
return $parsedContents; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
} |