|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Emoji\Util; |
|
6
|
|
|
|
|
7
|
|
|
class Property |
|
8
|
|
|
{ |
|
9
|
|
|
public const REGEX = '/^(?:(?P<emptyNullable>[!?]{2})|(?P<nullable>[!?]))??(?P<customType>[^[<]+)?(?P<type>array|bool|boolean|double|float|int|integer|null|object|string)??(?P<iterable>\[\])?(?:<(?P<callback>[^[]+)>)??$/Ui'; |
|
10
|
|
|
|
|
11
|
|
|
public const TYPES = ['array', 'bool', 'boolean', 'double', 'float', 'int', 'integer', 'null', 'object', 'string']; |
|
12
|
|
|
|
|
13
|
|
|
/** @var ?string */ |
|
14
|
|
|
public $callback; |
|
15
|
|
|
|
|
16
|
|
|
/** @var ?string */ |
|
17
|
|
|
public $customType; |
|
18
|
|
|
|
|
19
|
|
|
/** @var bool */ |
|
20
|
|
|
public $emptyNullable; |
|
21
|
|
|
|
|
22
|
|
|
/** @var bool */ |
|
23
|
|
|
public $nullable; |
|
24
|
|
|
|
|
25
|
|
|
/** @var bool */ |
|
26
|
|
|
public $iterable; |
|
27
|
|
|
|
|
28
|
|
|
/** @var bool */ |
|
29
|
|
|
public $isPhpType; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
public $type; |
|
33
|
|
|
|
|
34
|
156 |
|
public function __construct(string $type) |
|
35
|
|
|
{ |
|
36
|
156 |
|
\preg_match(self::REGEX, $type, $matches, PREG_UNMATCHED_AS_NULL); |
|
37
|
156 |
|
$this->callback = (string) ($matches['callback'] ?? null); |
|
38
|
156 |
|
$this->customType = (string) ($matches['customType'] ?? null); |
|
39
|
156 |
|
$this->emptyNullable = ! ! ($matches['emptyNullable'] ?? false); |
|
40
|
156 |
|
$this->iterable = ! ! ($matches['iterable'] ?? false); |
|
41
|
156 |
|
$this->nullable = ! ! ($this->emptyNullable || ($matches['nullable'] ?? false)); |
|
42
|
156 |
|
$this->type = (string) ($matches['type'] ?? 'mixed'); |
|
43
|
156 |
|
$this->isPhpType = \in_array($this->type, self::TYPES, true); |
|
44
|
156 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
156 |
|
public static function cast(string $type, $value) |
|
52
|
|
|
{ |
|
53
|
156 |
|
return (new self($type))->castValue($value); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $value |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed[] |
|
60
|
|
|
*/ |
|
61
|
75 |
|
protected function castIterable($value): array |
|
62
|
|
|
{ |
|
63
|
75 |
|
$value = (array) $value; |
|
64
|
75 |
|
$type = $this->nullable |
|
65
|
3 |
|
? \sprintf('?%s', $this->type) |
|
66
|
75 |
|
: $this->type; |
|
67
|
|
|
|
|
68
|
|
|
/** @var string[] $types */ |
|
69
|
75 |
|
$types = \array_fill_keys(\array_keys($value), $type); |
|
70
|
|
|
|
|
71
|
75 |
|
return Normalize::properties($value, $types); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param mixed $value |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
156 |
|
public function castValue($value) |
|
80
|
|
|
{ |
|
81
|
|
|
// Immediately return if nullable or empty. |
|
82
|
156 |
|
if (($this->nullable && $value === null) || ($this->emptyNullable && ! $value)) { |
|
83
|
93 |
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @psalm-var string $value */ |
|
87
|
129 |
|
$value = $this->castValueCallback($value); |
|
88
|
|
|
|
|
89
|
129 |
|
if ($this->iterable) { |
|
90
|
75 |
|
return $this->castIterable($value); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
126 |
|
return $this->castValueType($value); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param mixed $value |
|
98
|
|
|
* |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
129 |
|
protected function castValueCallback($value) |
|
102
|
|
|
{ |
|
103
|
129 |
|
if (($callback = $this->callback) && \is_callable($callback)) { |
|
104
|
|
|
/** @psalm-var string $value */ |
|
105
|
72 |
|
$value = $callback($value); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
129 |
|
return $value; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
*/ |
|
116
|
126 |
|
protected function castValueType($value) |
|
117
|
|
|
{ |
|
118
|
126 |
|
if (($type = $this->customType) && \class_exists($type)) { |
|
119
|
|
|
try { |
|
120
|
69 |
|
$ref = new \ReflectionClass($type); |
|
121
|
69 |
|
$value = $ref->newInstanceArgs([$value]); |
|
122
|
69 |
|
} catch (\ReflectionException $e) { |
|
123
|
|
|
// Intentionally left empty. |
|
124
|
|
|
} |
|
125
|
|
|
} else { |
|
126
|
123 |
|
Normalize::setType($value, $this->type); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
126 |
|
return $this->emptyNullable && ! $value |
|
130
|
3 |
|
? null |
|
131
|
126 |
|
: $value; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|