1 | <?php |
||
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) |
|
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) |
|
107 | } |
||
108 |