1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Caster; |
3
|
|
|
|
4
|
|
|
use Wandu\Caster\Caster\ArrayCaster; |
5
|
|
|
use Wandu\Caster\Caster\BooleanCatser; |
6
|
|
|
use Wandu\Caster\Caster\FloatCaster; |
7
|
|
|
use Wandu\Caster\Caster\IntegerCaster; |
8
|
|
|
use Wandu\Caster\Caster\StringCaster; |
9
|
|
|
|
10
|
|
|
class Caster implements CastManagerInterface |
11
|
|
|
{ |
12
|
|
|
/** @var \Wandu\Caster\CasterInterface[] */ |
13
|
|
|
protected $casters = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \Wandu\Caster\CasterInterface[] $casters |
17
|
|
|
*/ |
18
|
80 |
|
public function __construct(array $casters = []) |
19
|
|
|
{ |
20
|
80 |
|
$this->casters = $casters + $this->getDefaultCasters(); |
|
|
|
|
21
|
80 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
1 |
|
public function addCaster(string $type, CasterInterface $caster) |
27
|
|
|
{ |
28
|
1 |
|
$this->casters[$type] = $caster; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
80 |
|
public function cast($value, string $type) |
35
|
|
|
{ |
36
|
80 |
|
$originType = $type; |
37
|
80 |
|
if ($this->isNullable($type) && $value === null) { |
38
|
15 |
|
return null; |
39
|
|
|
} |
40
|
66 |
|
$type = rtrim($type, '?'); // strip nullable |
41
|
66 |
|
if ($this->isArrayable($type)) { |
42
|
23 |
|
$type = substr($type, 0, -2); // strip [] |
43
|
23 |
|
return array_map(function ($item) use ($type) { |
44
|
16 |
|
return $this->cast($item, $type); |
45
|
23 |
|
}, $this->getCaster('[]', $originType)->cast($value)); |
46
|
|
|
} |
47
|
59 |
|
return $this->getCaster($type, $originType)->cast($value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $type |
52
|
|
|
* @param string $originType |
53
|
|
|
* @return \Wandu\Caster\CasterInterface |
54
|
|
|
*/ |
55
|
66 |
|
protected function getCaster(string $type, string $originType): CasterInterface |
56
|
|
|
{ |
57
|
66 |
|
if (isset($this->casters[$type])) { |
58
|
65 |
|
return $this->casters[$type]; |
59
|
|
|
} |
60
|
2 |
|
throw new UnsupportTypeException($originType); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $type |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
66 |
|
protected function isArrayable($type): bool |
68
|
|
|
{ |
69
|
66 |
|
return substr($type, -2) === '[]'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $type |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
80 |
|
protected function isNullable($type): bool |
77
|
|
|
{ |
78
|
80 |
|
return substr($type, -1) === '?'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Wandu\Caster\CasterInterface[] |
83
|
|
|
*/ |
84
|
80 |
|
protected function getDefaultCasters() |
85
|
|
|
{ |
86
|
80 |
|
$integerCaster = new IntegerCaster(); |
87
|
80 |
|
$floatCaster = new FloatCaster(); |
88
|
80 |
|
$booleanCaster = new BooleanCatser(); |
89
|
80 |
|
$arrayCaster = new ArrayCaster(); |
90
|
|
|
return [ |
91
|
80 |
|
'string' => new StringCaster(), |
92
|
80 |
|
'int' => $integerCaster, |
93
|
80 |
|
'integer' => $integerCaster, |
94
|
80 |
|
'num' => $floatCaster, |
95
|
80 |
|
'number' => $floatCaster, |
96
|
80 |
|
'float' => $floatCaster, |
97
|
80 |
|
'double' => $floatCaster, |
98
|
80 |
|
'bool' => $booleanCaster, |
99
|
80 |
|
'boolean' => $booleanCaster, |
100
|
80 |
|
'array' => $arrayCaster, |
101
|
80 |
|
'[]' => $arrayCaster, // special caster |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..