-
-
Notifications
You must be signed in to change notification settings - Fork 999
Expand file tree
/
Copy pathCast.php
More file actions
206 lines (173 loc) · 7.36 KB
/
Copy pathCast.php
File metadata and controls
206 lines (173 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Leantime\Core\Support;
use Illuminate\Support\Str;
/**
* @todo the cast to method needs to be refactored to have better support of constructor params
**/
class Cast
{
protected array $mappings;
public function __construct(private array|object $object)
{
if (is_array($this->object)) {
$this->object = (object) $this->object;
}
}
/**
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws \ReflectionException
**/
public function castTo(string $classDest, array $constructParams = [], array $mappings = []): object
{
$this->mappings ??= $mappings;
if (! class_exists($classDest)) {
throw new \InvalidArgumentException(sprintf('Class %s does not exist.', $classDest));
}
$sourceObj = $this->object;
$classRef = new \ReflectionClass($classDest);
$properties = $classRef->getProperties();
if (
! empty($reflectedConstructParams = $classRef->getConstructor()?->getParameters() ?? [])
&& empty($constructParams)
) {
foreach ($reflectedConstructParams as $param) {
if (isset($sourceObj->{$param->getName()})) {
$constructParams[] = $sourceObj->{$param->getName()};
continue;
}
if ($param->isOptional()) {
$constructParams[] = $param->getDefaultValue();
continue;
}
throw new \InvalidArgumentException(sprintf('Missing construct parameter %s.', $param->getName()));
}
}
$returnObj = build(new $classDest(...$constructParams));
foreach ($properties as $property) {
$name = $property->getName();
if (! isset($sourceObj->$name)) {
if ($property->hasDefaultValue()) {
$returnObj->set($name, $property->getDefaultValue());
continue;
}
throw new \RuntimeException(sprintf('Property %s does not exist in source object.', $name));
}
try {
$type = collect($mappings)->firstOrFail(fn ($mapping, $key) => in_array($key, [$name, '*']));
} catch (\Illuminate\Support\ItemNotFoundException) {
$type = ($reflectionType = $property->getType()) instanceof \ReflectionNamedType ? $reflectionType->getName() : null;
}
$returnObj->set($name, match (true) {
enum_exists($type) => self::castEnum($sourceObj->$name, $type),
$type !== 'stdClass' && class_exists($type) => (new self($sourceObj->$name))->castTo(
classDest: $type,
mappings: $this->getMatchingMappings($mappings, $name),
),
in_array($type, ['array', 'object', 'stdClass']) => $this->handleIterator(
$sourceObj->$name,
$this->getMatchingMappings($mappings, $name)
),
is_null($type) || $type == 'mixed' => $sourceObj->$name,
default => self::castSimple($sourceObj->$name, $type),
});
}
return $returnObj->get();
}
/**
* @throws \RuntimeException
* @throws \InvalidArgumentException
**/
public static function castSimple(mixed $value, string $simpleType): mixed
{
if (
is_null($castedValue = match ($simpleType) {
'int', 'integer' => filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE),
'float' => filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE),
'string', 'str' => is_array($value) || (is_object($value) && ! method_exists($value, '__toString')) ? null : (string) $value,
'bool', 'boolean' => filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
'object', 'stdClass' => is_array($value) ? (object) $value : null,
'array' => is_object($value) || is_array($value) ? (array) $value : null,
default => throw new \InvalidArgumentException(sprintf('%s is not a simple type.', $simpleType)),
})
) {
throw new \RuntimeException(sprintf('Could not cast value to type %s.', $simpleType));
}
return $castedValue;
}
/**
* Cast to backed enum
*
**/
public static function castEnum(mixed $value, string $enumClass): mixed
{
// For non-backed enums, iterate and match by name.
if (is_string($value)) {
foreach ($enumClass::cases() as $case) {
if ($case->name === $value) {
return $case;
}
}
}
// For backed enums, try to get the case by value
if (
is_subclass_of($enumClass, \BackedEnum::class)
&& ($enum = $enumClass::tryFrom($value) ?? false)
) {
return $enum;
}
throw new \InvalidArgumentException(sprintf('Value cannot be casted to %s.', $enumClass));
}
/**
* Casts a string value into a datetime object.
*
* @param string $value The value to be casted into a datetime object.
* @return \Carbon\CarbonImmutable The datetime object.
*
* @throws \InvalidArgumentException If the value is not a valid datetime string.
**/
public static function castDateTime(string $value)
{
if (is_string($value)) {
return dtHelper()->parseDbDateTime($value);
}
throw new \InvalidArgumentException('Value cannot be casted datetime');
}
protected function handleIterator(iterable $iterator, array $mappings = []): array|object
{
$result = is_object($iterator) ? new \stdClass : [];
foreach ($iterator as $key => $value) {
if (is_numeric($key)) {
$type = $mappings['*'] ?? false;
} else {
if ($type = preg_match('/\<[a-zA-Z0-9\\\\]+\>/', $key)) {
$key = preg_replace('/\<[a-zA-Z0-9\\\\]+\>/', '', $key);
} else {
$type = $mappings[$key] ?? $mappings['*'] ?? false;
}
}
$value = match (true) {
$type && enum_exists($type) => self::castEnum($value, $type),
$type && class_exists($type) => (new self($value))->castTo($type),
$type && in_array($type, ['string', 'str', 'int', 'integer', 'float', 'bool', 'boolean']) => self::castSimple($value, $type),
$type && in_array($type, ['array', 'object', 'stdClass']),
is_array($value),
is_object($value) => $this->{__FUNCTION__}($value, $this->getMatchingMappings($mappings, (string) $key)),
default => $value,
};
if (is_object($result)) {
$result->$key = $value;
} else {
$result[$key] = $value;
}
}
return $result;
}
protected function getMatchingMappings(array $mappings, string $propName): array
{
return collect($mappings)
->filter(fn ($mapping, $key) => Str::startsWith($key, "$propName.") || Str::startsWith($key, '*.'))
->mapWithKeys(fn ($mapping, $key) => [Str::after($key, '.') => $mapping])
->all();
}
}