Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.22% |
122 / 154 |
|
35.71% |
5 / 14 |
CRAP | |
0.00% |
0 / 1 |
Utils | |
79.22% |
122 / 154 |
|
35.71% |
5 / 14 |
144.81 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toArray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
toString | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
9.49 | |||
toInt | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
toFloat | |
44.44% |
4 / 9 |
|
0.00% |
0 / 1 |
4.54 | |||
toBoolean | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
toMixed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
toDate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
toNative | |
60.00% |
12 / 20 |
|
0.00% |
0 / 1 |
21.22 | |||
toEnum | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
toTypehint | |
82.86% |
29 / 35 |
|
0.00% |
0 / 1 |
23.22 | |||
sortTypes | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
displayMixedAsString | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
8 | |||
getDisplayNameForValueObject | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | namespace Apie\Core\ValueObjects; |
3 | |
4 | use Apie\Core\Attributes\ResourceName; |
5 | use Apie\Core\Exceptions\InvalidTypeException; |
6 | use Apie\Core\Lists\ItemHashmap; |
7 | use Apie\Core\Lists\ItemList; |
8 | use Apie\Core\ValueObjects\Interfaces\TimeRelatedValueObjectInterface; |
9 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
10 | use BackedEnum; |
11 | use DateTime; |
12 | use DateTimeImmutable; |
13 | use DateTimeInterface; |
14 | use ReflectionClass; |
15 | use ReflectionNamedType; |
16 | use ReflectionUnionType; |
17 | use stdClass; |
18 | use Stringable; |
19 | use UnitEnum; |
20 | |
21 | /** |
22 | * Util classes used by or from value objects. |
23 | */ |
24 | final class Utils |
25 | { |
26 | private function __construct() |
27 | { |
28 | } |
29 | |
30 | /** |
31 | * @return mixed[] |
32 | */ |
33 | public static function toArray(mixed $input): array |
34 | { |
35 | if (is_array($input)) { |
36 | return $input; |
37 | } |
38 | if ($input instanceof ItemList || $input instanceof ItemHashmap) { |
39 | return $input->toArray(); |
40 | } |
41 | if (is_iterable($input)) { |
42 | return iterator_to_array($input); |
43 | } |
44 | throw new InvalidTypeException($input, 'array'); |
45 | } |
46 | |
47 | public static function toString(mixed $input): string |
48 | { |
49 | if ($input instanceof ValueObjectInterface) { |
50 | $input = $input instanceof Stringable ? $input->__toString() : $input->toNative(); |
51 | } |
52 | if (is_array($input)) { |
53 | throw new InvalidTypeException($input, 'string'); |
54 | } |
55 | if (! $input instanceof Stringable && $input instanceof BackedEnum) { |
56 | return (string) $input->value; |
57 | } |
58 | if (! $input instanceof Stringable && $input instanceof UnitEnum) { |
59 | return (string) $input->name; |
60 | } |
61 | if ($input instanceof DateTimeInterface) { |
62 | return $input->format(DateTimeInterface::ATOM); |
63 | } |
64 | return (string) $input; |
65 | } |
66 | |
67 | public static function toInt(mixed $input): int |
68 | { |
69 | if ($input instanceof ValueObjectInterface) { |
70 | $input = $input->toNative(); |
71 | } |
72 | if (is_array($input)) { |
73 | throw new InvalidTypeException($input, 'int'); |
74 | } |
75 | $iInput = (int) $input; |
76 | $sInput = (string) $input; |
77 | if ($sInput !== ((string) $iInput)) { |
78 | throw new InvalidTypeException( |
79 | $input, |
80 | 'int' |
81 | ); |
82 | } |
83 | return (int) $input; |
84 | } |
85 | |
86 | public static function toFloat(mixed $input): float |
87 | { |
88 | if ($input instanceof ValueObjectInterface) { |
89 | $input = $input->toNative(); |
90 | } |
91 | $inputString = trim(self::toString($input)); |
92 | if (!preg_match('/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/', $inputString)) { |
93 | throw new InvalidTypeException( |
94 | $inputString, |
95 | 'float' |
96 | ); |
97 | } |
98 | return (float) $input; |
99 | } |
100 | |
101 | public static function toBoolean(mixed $input): bool |
102 | { |
103 | if ($input instanceof ValueObjectInterface) { |
104 | $input = $input->toNative(); |
105 | } |
106 | return (bool) $input; |
107 | } |
108 | |
109 | public static function toMixed(mixed $input): mixed |
110 | { |
111 | return $input instanceof ValueObjectInterface ? $input->toNative() : $input; |
112 | } |
113 | |
114 | public static function toDate(mixed $input, string $class = DateTimeImmutable::class): DateTimeInterface |
115 | { |
116 | if ($class === DateTimeInterface::class) { |
117 | $class = DateTimeImmutable::class; |
118 | } |
119 | if ($input instanceof DateTimeInterface) { |
120 | return $class::createFromInterface($input); |
121 | } |
122 | if ($input instanceof TimeRelatedValueObjectInterface) { |
123 | return $input->toDate(); |
124 | } |
125 | return $class::createFromFormat(DateTime::ATOM, self::toString($input)); |
126 | } |
127 | |
128 | /** |
129 | * @return mixed[]|string|int|float|bool|UnitEnum|null |
130 | */ |
131 | public static function toNative(mixed $input): array|string|int|float|bool|UnitEnum|null |
132 | { |
133 | if ($input instanceof ValueObjectInterface) { |
134 | $input = $input->toNative(); |
135 | } |
136 | if ($input instanceof stdClass) { |
137 | $input = json_decode(json_encode($input), true); |
138 | } |
139 | if (is_iterable($input)) { |
140 | $result = []; |
141 | foreach ($input as $key => $value) { |
142 | $result[$key] = self::toNative($value); |
143 | } |
144 | return $result; |
145 | } |
146 | if ($input instanceof UnitEnum) { |
147 | return $input; |
148 | } |
149 | if (is_object($input) && $input instanceof Stringable) { |
150 | return (string) $input; |
151 | } |
152 | if (is_bool($input)) { |
153 | return $input; |
154 | } |
155 | if (is_string($input) || is_numeric($input)) { |
156 | return $input; |
157 | } |
158 | if (null === $input) { |
159 | return null; |
160 | } |
161 | throw new InvalidTypeException($input, 'ValueObject|array|string|int|float|bool|UnitEnum|null'); |
162 | } |
163 | |
164 | public static function toEnum(string $className, mixed $input): UnitEnum |
165 | { |
166 | if ($input instanceof $className) { |
167 | return $input; |
168 | } |
169 | |
170 | $input = self::toString($input); |
171 | foreach ($className::cases() as $enum) { |
172 | if ($enum->value === $input || $enum->name === $input) { |
173 | return $enum; |
174 | } |
175 | } |
176 | throw new InvalidTypeException($input, self::getDisplayNameForValueObject(new ReflectionClass($className))); |
177 | } |
178 | |
179 | /** |
180 | * Converts native value in typehint. |
181 | */ |
182 | public static function toTypehint( |
183 | ReflectionUnionType|ReflectionNamedType $typehint, |
184 | mixed $input |
185 | ): mixed { |
186 | if ($input === null) { |
187 | if ($typehint->allowsNull()) { |
188 | return null; |
189 | } |
190 | throw InvalidTypeException::fromTypehint($input, $typehint); |
191 | } |
192 | $types = $typehint instanceof ReflectionUnionType ? self::sortTypes($input, ...$typehint->getTypes()) : [$typehint]; |
193 | $lastError = new InvalidTypeException($input, '(unknown)'); |
194 | foreach ($types as $type) { |
195 | try { |
196 | if ($type->isBuiltin()) { |
197 | switch ($type->getName()) { |
198 | case 'string': |
199 | return self::toString($input); |
200 | case 'int': |
201 | return self::toInt($input); |
202 | case 'float': |
203 | return self::toFloat($input); |
204 | case 'array': |
205 | case 'iterable': |
206 | return self::toArray($input); |
207 | case 'bool': |
208 | return self::toBoolean($input); |
209 | case 'mixed': |
210 | return self::toMixed($input); |
211 | default: |
212 | throw new InvalidTypeException($input, $type->getName()); |
213 | } |
214 | } |
215 | $className = $type->getName(); |
216 | switch ($className) { |
217 | case stdClass::class: |
218 | return json_decode(json_encode(self::toArray($input)), false); |
219 | case DateTimeInterface::class: |
220 | case DateTimeImmutable::class: |
221 | case DateTime::class: |
222 | return self::toDate($input, $className); |
223 | } |
224 | $refl = new ReflectionClass($className); |
225 | if ($refl->implementsInterface(ValueObjectInterface::class)) { |
226 | return $className::fromNative($input); |
227 | } |
228 | if ($refl->implementsInterface(UnitEnum::class)) { |
229 | return self::toEnum($className, $input); |
230 | } |
231 | throw new InvalidTypeException($className, 'ValueObjectInterface'); |
232 | } catch (InvalidTypeException $error) { |
233 | $lastError = $error; |
234 | } |
235 | } |
236 | throw InvalidTypeException::chainException($lastError); |
237 | } |
238 | |
239 | /** |
240 | * Sort typehints in a specific order. |
241 | * |
242 | * @return array<int, ReflectionNamedType> |
243 | */ |
244 | public static function sortTypes(mixed $input, ReflectionNamedType... $types): array |
245 | { |
246 | $prio = [ |
247 | 'string' => gettype($input) === 'string' ? -3 : 1, |
248 | 'int' => 0, |
249 | 'float' => -1, |
250 | ]; |
251 | $callback = function (ReflectionNamedType $type1, ReflectionNamedType $type2) use (&$prio) : int { |
252 | $name1 = $type1->getName(); |
253 | $name2 = $type2->getName(); |
254 | $prio1 = $prio[$name1] ?? -2; |
255 | $prio2 = $prio[$name2] ?? -2; |
256 | return $prio1 <=> $prio2; |
257 | }; |
258 | |
259 | usort($types, $callback); |
260 | return $types; |
261 | } |
262 | |
263 | public static function displayMixedAsString(mixed $input): string |
264 | { |
265 | if ($input === null) { |
266 | return '(null)'; |
267 | } |
268 | if (is_object($input)) { |
269 | if ($input instanceof Stringable) { |
270 | return '(object "' . $input->__toString() . '")'; |
271 | } |
272 | if ($input instanceof DateTimeInterface) { |
273 | return $input->format(DateTime::ATOM); |
274 | } |
275 | return '(object ' . self::getDisplayNameForValueObject(new ReflectionClass($input)) . ')'; |
276 | } |
277 | |
278 | if (is_bool($input)) { |
279 | return json_encode($input); |
280 | } |
281 | |
282 | if (is_string($input) || is_numeric($input)) { |
283 | return (string) '"' . $input . '"'; |
284 | } |
285 | |
286 | return gettype($input); |
287 | } |
288 | |
289 | /** |
290 | * @param ValueObjectInterface|ReflectionClass<object> $class |
291 | */ |
292 | public static function getDisplayNameForValueObject(ValueObjectInterface|ReflectionClass $class): string |
293 | { |
294 | if ($class instanceof ReflectionClass) { |
295 | $className = $class->getShortName(); |
296 | $refl = $class; |
297 | } else { |
298 | $refl = new ReflectionClass($class); |
299 | $className = $refl->getShortName(); |
300 | } |
301 | foreach ($refl->getAttributes(ResourceName::class) as $attribute) { |
302 | return $attribute->newInstance()->name; |
303 | } |
304 | if (strcasecmp($className, 'Abstract') === 0 || strcasecmp($className, 'AbstractInterface') === 0) { |
305 | return 'Abstract'; |
306 | } |
307 | return preg_replace( |
308 | '/Interface/i', |
309 | '', |
310 | preg_replace('/^abstract/i', '', $className) |
311 | ); |
312 | } |
313 | } |