1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Indigo\Ini; |
4
|
|
|
|
5
|
|
|
use Indigo\Ini\Exception\ParserException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parses an INI string. |
9
|
|
|
* |
10
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Parser |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Parses an INI string. |
16
|
|
|
* |
17
|
|
|
* @param string $ini |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
14 |
|
public function parse($ini) |
22
|
|
|
{ |
23
|
14 |
|
if (!is_string($ini)) { |
24
|
1 |
|
throw new ParserException('Cannot parse non-string INI data'); |
25
|
|
|
} |
26
|
|
|
|
27
|
13 |
|
$ini = @parse_ini_string($ini, true, INI_SCANNER_RAW); |
28
|
|
|
|
29
|
13 |
|
if (false === $ini) { |
30
|
|
|
$e = error_get_last(); |
31
|
|
|
throw new ParserException('Error during parsing INI: '.$e['message']); |
32
|
|
|
} |
33
|
|
|
|
34
|
13 |
|
return $this->normalize($ini); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Normalizes INI and other values. |
39
|
|
|
* |
40
|
|
|
* @param mixed $value |
41
|
|
|
* |
42
|
|
|
* @return bool|int|null|string|array |
43
|
|
|
*/ |
44
|
13 |
|
private function normalize($value) |
45
|
|
|
{ |
46
|
|
|
// Normalize array values |
47
|
13 |
|
if (is_array($value)) { |
48
|
13 |
|
foreach ($value as &$subValue) { |
49
|
13 |
|
$subValue = $this->normalize($subValue); |
50
|
13 |
|
} |
51
|
|
|
|
52
|
13 |
|
return $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Don't normalize non-string value |
56
|
13 |
|
if (!is_string($value)) { |
57
|
|
|
return $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Normalize true boolean value |
61
|
13 |
|
if ($this->compareValues($value, ['true', 'on', 'yes'])) { |
62
|
4 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Normalize false boolean value |
66
|
13 |
|
if ($this->compareValues($value, ['false', 'off', 'no', 'none'])) { |
67
|
4 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Normalize null value |
71
|
9 |
|
if ($this->compareValues($value, ['null'])) { |
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Normalize numeric value |
76
|
8 |
|
if (is_numeric($value)) { |
77
|
2 |
|
$numericValue = $value + 0; |
78
|
|
|
|
79
|
2 |
|
if ((is_int($numericValue) && (int) $value === $numericValue) |
80
|
1 |
|
|| (is_float($numericValue) && (float) $value === $numericValue) |
81
|
2 |
|
) { |
82
|
2 |
|
$value = $numericValue; |
83
|
2 |
|
} |
84
|
2 |
|
} |
85
|
|
|
|
86
|
8 |
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Case insensitively compares values. |
91
|
|
|
* |
92
|
|
|
* @param string $value |
93
|
|
|
* @param array $comparisons |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
13 |
|
private function compareValues($value, array $comparisons) |
98
|
|
|
{ |
99
|
13 |
|
foreach ($comparisons as $comparison) { |
100
|
13 |
|
if (0 === strcasecmp($value, $comparison)) { |
101
|
5 |
|
return true; |
102
|
|
|
} |
103
|
13 |
|
} |
104
|
|
|
|
105
|
13 |
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|