Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
8 / 10 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
ValueObjectSchemaProvider | |
80.00% |
8 / 10 |
|
75.00% |
3 / 4 |
6.29 | |
0.00% |
0 / 1 |
supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
addDisplaySchemaFor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addCreationSchemaFor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSchema | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
2.09 |
1 | <?php |
2 | namespace Apie\SchemaGenerator\SchemaProviders; |
3 | |
4 | use Apie\Core\RegexUtils; |
5 | use Apie\Core\ValueObjects\CompositeValueObject; |
6 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
7 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
8 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
9 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
10 | use cebe\openapi\spec\Components; |
11 | use ReflectionClass; |
12 | |
13 | /** |
14 | * Gets schema data from the toNative() return type hint. |
15 | * @implements SchemaProvider<ValueObjectInterface> |
16 | */ |
17 | class ValueObjectSchemaProvider implements SchemaProvider |
18 | { |
19 | public function supports(ReflectionClass $class): bool |
20 | { |
21 | return $class->implementsInterface(ValueObjectInterface::class) && !in_array(CompositeValueObject::class, $class->getTraitNames()); |
22 | } |
23 | |
24 | public function addDisplaySchemaFor( |
25 | ComponentsBuilder $componentsBuilder, |
26 | string $componentIdentifier, |
27 | ReflectionClass $class, |
28 | bool $nullable = false |
29 | ): Components { |
30 | return $this->getSchema($componentsBuilder, $componentIdentifier, $class, true, $nullable); |
31 | } |
32 | |
33 | public function addCreationSchemaFor( |
34 | ComponentsBuilder $componentsBuilder, |
35 | string $componentIdentifier, |
36 | ReflectionClass $class, |
37 | bool $nullable = false |
38 | ): Components { |
39 | return $this->getSchema($componentsBuilder, $componentIdentifier, $class, false, $nullable); |
40 | } |
41 | |
42 | /** |
43 | * @param ReflectionClass<ValueObjectInterface> $class |
44 | */ |
45 | private function getSchema( |
46 | ComponentsBuilder $componentsBuilder, |
47 | string $componentIdentifier, |
48 | ReflectionClass $class, |
49 | bool $display, |
50 | bool $nullable, |
51 | ): Components { |
52 | $type = $class->getMethod('toNative')->getReturnType(); |
53 | $schema = $componentsBuilder->getSchemaForType($type, false, $display, $nullable); |
54 | |
55 | if ($class->implementsInterface(HasRegexValueObjectInterface::class)) { |
56 | $className = $class->name; |
57 | $schema->pattern = RegexUtils::removeDelimiters($className::getRegularExpression()); |
58 | } |
59 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
60 | |
61 | return $componentsBuilder->getComponents(); |
62 | } |
63 | } |