Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
65.00% |
13 / 20 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
ValueObjectUtils | |
65.00% |
13 / 20 |
|
25.00% |
1 / 4 |
26.98 | |
0.00% |
0 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
toNativeType | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
isValueObject | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
isNonCompositeValueObject | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
isCompositeValueObject | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | namespace Apie\Core\Utils; |
3 | |
4 | use Apie\Core\ValueObjects\CompositeValueObject; |
5 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
6 | use ReflectionClass; |
7 | use ReflectionMethod; |
8 | use ReflectionProperty; |
9 | use ReflectionType; |
10 | |
11 | final class ValueObjectUtils |
12 | { |
13 | /** |
14 | * @codeCoverageIgnore |
15 | */ |
16 | private function __construct() |
17 | { |
18 | } |
19 | |
20 | /** |
21 | * @param string|ReflectionClass<ValueObjectInterface>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
22 | */ |
23 | public static function toNativeType(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): ReflectionType |
24 | { |
25 | $class = ConverterUtils::toReflectionClass($input); |
26 | return ConverterUtils::toReflectionType($class->getMethod('toNative')); |
27 | } |
28 | |
29 | /** |
30 | * @param string|ReflectionClass<object>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
31 | */ |
32 | public static function isValueObject(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): bool |
33 | { |
34 | $class = ConverterUtils::toReflectionClass($input); |
35 | if (!$class || in_array(ValueObjectInterface::class, $class->getInterfaceNames()) || $class->isAbstract()) { |
36 | return false; |
37 | } |
38 | return true; |
39 | } |
40 | |
41 | /** |
42 | * @param string|ReflectionClass<object>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
43 | */ |
44 | public static function isNonCompositeValueObject(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): bool |
45 | { |
46 | $class = ConverterUtils::toReflectionClass($input); |
47 | if (!$class || !in_array(ValueObjectInterface::class, $class->getInterfaceNames())) { |
48 | return false; |
49 | } |
50 | |
51 | if (in_array(CompositeValueObject::class, $class->getTraitNames())) { |
52 | return false; |
53 | } |
54 | $parentClass = $class->getParentClass(); |
55 | |
56 | return $parentClass ? self::isNonCompositeValueObject($parentClass) : true; |
57 | } |
58 | |
59 | /** |
60 | * @param string|ReflectionClass<object>|ReflectionProperty|ReflectionType|ReflectionMethod $input |
61 | */ |
62 | public static function isCompositeValueObject(string|ReflectionClass|ReflectionProperty|ReflectionType|ReflectionMethod $input): bool |
63 | { |
64 | $class = ConverterUtils::toReflectionClass($input); |
65 | if (!$class || !in_array(ValueObjectInterface::class, $class->getInterfaceNames())) { |
66 | return false; |
67 | } |
68 | |
69 | if (in_array(CompositeValueObject::class, $class->getTraitNames())) { |
70 | return true; |
71 | } |
72 | $parentClass = $class->getParentClass(); |
73 | |
74 | return $parentClass ? self::isCompositeValueObject($parentClass) : false; |
75 | } |
76 | } |