Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.24% |
60 / 68 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
PropertyToFieldMetadataUtil | |
88.24% |
60 / 68 |
|
60.00% |
3 / 5 |
33.67 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fromPropertyStringToFieldMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fromPropertyArrayToFieldMetadata | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
hasPropertyWithType | |
84.44% |
38 / 45 |
|
0.00% |
0 / 1 |
23.82 | |||
visit | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | namespace Apie\Core; |
3 | |
4 | use Apie\Core\Context\ApieContext; |
5 | use Apie\Core\Lists\ReflectionTypeSet; |
6 | use Apie\Core\Metadata\Fields\FieldInterface; |
7 | use Apie\Core\Metadata\MetadataFactory; |
8 | use Apie\Core\Metadata\MetadataInterface; |
9 | use Apie\Core\Utils\ConverterUtils; |
10 | use ReflectionClass; |
11 | use ReflectionIntersectionType; |
12 | use ReflectionNamedType; |
13 | use ReflectionType; |
14 | use ReflectionUnionType; |
15 | |
16 | final class PropertyToFieldMetadataUtil |
17 | { |
18 | private function __construct() |
19 | { |
20 | } |
21 | |
22 | /** |
23 | * @param ReflectionClass<object> $class |
24 | */ |
25 | public static function fromPropertyStringToFieldMetadata( |
26 | ReflectionClass $class, |
27 | ApieContext $apieContext, |
28 | string $property |
29 | ): ?FieldInterface { |
30 | return self::fromPropertyArrayToFieldMetadata($class, $apieContext, explode('.', $property)); |
31 | } |
32 | |
33 | /** |
34 | * @param ReflectionClass<object> $class |
35 | * @param array<int, string> $property |
36 | */ |
37 | public static function fromPropertyArrayToFieldMetadata( |
38 | ReflectionClass $class, |
39 | ApieContext $apieContext, |
40 | array $property |
41 | ): ?FieldInterface { |
42 | $root = $apieContext->hasContext('id') |
43 | ? MetadataFactory::getModificationMetadata($class, $apieContext) |
44 | : MetadataFactory::getCreationMetadata($class, $apieContext); |
45 | return self::visit($root, $apieContext, $property); |
46 | } |
47 | |
48 | public static function hasPropertyWithType( |
49 | ReflectionType $input, |
50 | ReflectionNamedType $searchType, |
51 | ApieContext $apieContext, |
52 | ReflectionTypeSet $visitedTypes = new ReflectionTypeSet() |
53 | ): bool { |
54 | if (isset($visitedTypes[$input])) { |
55 | return false; |
56 | } |
57 | $visitedTypes[] = $input; |
58 | if ($input instanceof ReflectionUnionType || $input instanceof ReflectionIntersectionType) { |
59 | foreach ($input->getTypes() as $type) { |
60 | if (self::hasPropertyWithType($type, $searchType, $apieContext, $visitedTypes->clone())) { |
61 | return true; |
62 | } |
63 | } |
64 | return false; |
65 | } |
66 | assert($input instanceof ReflectionNamedType); |
67 | if ($input->isBuiltin()) { |
68 | switch ($input->getName()) { |
69 | case 'null': |
70 | return $searchType->allowsNull(); |
71 | case 'int': |
72 | return in_array($searchType->getName(), ['int', 'float']); |
73 | case 'true': |
74 | case 'false': |
75 | return in_array($searchType->getName(), [$input->getName(), 'bool']); |
76 | case 'bool': |
77 | return in_array($searchType->getName(), ['true', 'false', 'bool']); |
78 | case 'array': |
79 | return in_array($searchType->getName(), ['string', 'true', 'false', 'bool', 'int', 'float', 'null']); |
80 | default: |
81 | return $searchType->getName() === $input->getName(); |
82 | } |
83 | } |
84 | $class = ConverterUtils::toReflectionClass($input); |
85 | if ($class === null) { |
86 | return false; |
87 | } |
88 | if (in_array($searchType->getName(), $class->getInterfaceNames())) { |
89 | return true; |
90 | } |
91 | $ptr = $class; |
92 | while ($ptr) { |
93 | if ($class->name === $searchType->getName()) { |
94 | return true; |
95 | } |
96 | $ptr = $ptr->getParentClass(); |
97 | } |
98 | $metadata = MetadataFactory::getResultMetadata($class, $apieContext); |
99 | $hashmap = $metadata->getHashmap(); |
100 | foreach ($hashmap as $getter) { |
101 | $typehint = $getter->getTypehint(); |
102 | if (!$typehint) { |
103 | return true; |
104 | } |
105 | if (self::hasPropertyWithType($typehint, $searchType, $apieContext, $visitedTypes->clone())) { |
106 | return true; |
107 | } |
108 | } |
109 | $arrayItemType = ConverterUtils::toReflectionType($metadata->getArrayItemType()?->toClass() ?? 'null'); |
110 | if ($arrayItemType) { |
111 | return self::hasPropertyWithType($arrayItemType, $searchType, $apieContext, $visitedTypes->clone()); |
112 | } |
113 | |
114 | return false; |
115 | } |
116 | |
117 | /** |
118 | * @param array<int, string> $property |
119 | */ |
120 | private static function visit(MetadataInterface $node, ApieContext $apieContext, array $property): ?FieldInterface |
121 | { |
122 | $hashmap = $node->getHashmap(); |
123 | $key = array_shift($property); |
124 | if (isset($hashmap[$key])) { |
125 | if (empty($property)) { |
126 | return $hashmap[$key]; |
127 | } |
128 | $type = $hashmap[$key]->getTypehint(); |
129 | if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) { |
130 | $class = new ReflectionClass($type->getName()); |
131 | return self::visit( |
132 | MetadataFactory::getCreationMetadata($class, $apieContext), |
133 | $apieContext, |
134 | $property |
135 | ); |
136 | } |
137 | } |
138 | $arrayItemType = $node->getArrayItemType(); |
139 | if (null === $arrayItemType) { |
140 | return null; |
141 | } |
142 | return self::visit($arrayItemType, $apieContext, $property); |
143 | } |
144 | } |