1 | <?php |
||
14 | class Parser |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $config; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $defaultDelimiter = ','; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $defaultEnclosure = '"'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $defaultEscape = '\\'; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $customTypes = []; |
||
40 | |||
41 | /** |
||
42 | * Parser constructor. |
||
43 | * |
||
44 | * @param array $config |
||
45 | */ |
||
46 | public function __construct(array $config) |
||
50 | |||
51 | /** |
||
52 | * Register a handler for a custom type. The handler will be called with |
||
53 | * the value to parse. |
||
54 | * |
||
55 | * @param string $type |
||
56 | * @param callable $callback |
||
57 | */ |
||
58 | public static function registerType($type, callable $callback) |
||
62 | |||
63 | /** |
||
64 | * @param $input |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public function fromString($input) |
||
72 | |||
73 | /** |
||
74 | * @param string $filename |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function fromFile($filename) |
||
82 | |||
83 | /** |
||
84 | * @param array $columns |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function parseRow(array $columns) |
||
100 | |||
101 | /** |
||
102 | * @param Reader $reader |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | protected function parse(Reader $reader) |
||
116 | |||
117 | /** |
||
118 | * @param string $key |
||
119 | * @param string $default |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | protected function getConfigValue($key, $default) |
||
127 | |||
128 | /** |
||
129 | * @param string $type |
||
130 | * @param string $value |
||
131 | * |
||
132 | * @return mixed |
||
133 | * |
||
134 | * @throws UnsupportedTypeException |
||
135 | */ |
||
136 | protected function getValue($type, $value) |
||
150 | |||
151 | /** |
||
152 | * @param string $type |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function parseType($type) |
||
166 | |||
167 | /** |
||
168 | * @param string $type |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function getMethodName($type) |
||
176 | |||
177 | /** |
||
178 | * @param string $value |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function parseString($value) |
||
186 | |||
187 | /** |
||
188 | * @param string $value |
||
189 | * |
||
190 | * @return int |
||
191 | * |
||
192 | * @throws CastException |
||
193 | */ |
||
194 | protected function parseInt($value) |
||
200 | |||
201 | /** |
||
202 | * @param string $value |
||
203 | * |
||
204 | * @return float |
||
205 | * |
||
206 | * @throws CastException |
||
207 | */ |
||
208 | protected function parseFloat($value) |
||
214 | |||
215 | /** |
||
216 | * @param string $string |
||
217 | * @param string $delimiter |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function parseArray($string, $delimiter) |
||
225 | |||
226 | /** |
||
227 | * @param string $value |
||
228 | * @param string $targetType |
||
229 | * |
||
230 | * @throws CastException |
||
231 | */ |
||
232 | protected function guardAgainstNonNumeric($value, $targetType) |
||
238 | |||
239 | /** |
||
240 | * @param string $type |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | protected function hasCustomType($type) |
||
248 | } |
||
249 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: