1 | <?php |
||
4 | class Caster implements CasterInterface |
||
5 | { |
||
6 | /** @var array */ |
||
7 | protected static $supportCasts = [ |
||
8 | 'string', 'int', 'integer', 'num', 'number', 'float', 'double', 'bool', 'boolean' |
||
9 | ]; |
||
10 | |||
11 | /** @var array */ |
||
12 | protected $boolFalse = [ |
||
13 | '0', 'false', 'False', 'FALSE', 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF' |
||
14 | ]; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | 40 | public function cast($value, $type) |
|
20 | { |
||
21 | 40 | if (($p = strpos($type, '[]')) !== false) { |
|
22 | 9 | $value = $this->normalizeArray($value); |
|
23 | 9 | $type = substr($type, 0, $p); |
|
24 | 9 | return array_map(function ($item) use ($type) { |
|
25 | 9 | return $this->cast($item, $type); |
|
26 | 9 | }, $value); |
|
27 | } |
||
28 | 40 | if (strpos($type, '?') !== false) { |
|
29 | 7 | if ($value === null) { |
|
30 | 7 | return null; |
|
31 | } |
||
32 | $type = str_replace($type, '?', ''); |
||
33 | } |
||
34 | 33 | return $this->castPlainType($type, $value); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param mixed $value |
||
39 | * @return array |
||
40 | */ |
||
41 | 9 | private function normalizeArray($value) |
|
51 | |||
52 | 33 | private function castPlainType($type, $value) |
|
97 | } |
||
98 |