Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.65% covered (warning)
67.65%
23 / 34
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AliasSchemaProvider
67.65% covered (warning)
67.65%
23 / 34
75.00% covered (warning)
75.00%
3 / 4
13.39
0.00% covered (danger)
0.00%
0 / 1
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addDisplaySchemaFor
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 addCreationSchemaFor
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 addModificationSchemaFor
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Apie\SchemaGenerator\SchemaProviders;
4
5use Apie\Core\ApieLib;
6use Apie\SchemaGenerator\Builders\ComponentsBuilder;
7use Apie\SchemaGenerator\Interfaces\ModifySchemaProvider;
8use Apie\TypeConverter\ReflectionTypeFactory;
9use cebe\openapi\spec\Components;
10use cebe\openapi\spec\Schema;
11use ReflectionClass;
12
13/**
14 * Get OpenAPI schema from the apie/core MetadataFactory class.
15 *
16 * @implements ModifySchemaProvider<object>
17 */
18class AliasSchemaProvider implements ModifySchemaProvider
19{
20    public function supports(ReflectionClass $class): bool
21    {
22        return ApieLib::hasAlias($class->name);
23    }
24
25    public function addDisplaySchemaFor(ComponentsBuilder $componentsBuilder, string $componentIdentifier, ReflectionClass $class, bool $nullable = false): Components
26    {
27        $alias = ApieLib::getAlias($class->name);
28        $type = ReflectionTypeFactory::createReflectionType($alias);
29        $types = $type instanceof \ReflectionUnionType ? $type->getTypes() : [$type];
30        $oneOfs = [];
31        foreach ($types as $type) {
32            $oneOfs[] = $componentsBuilder->getSchemaForType($type, false, true, $nullable);
33        }
34        $schema = new Schema([
35            'oneOf' => $oneOfs
36        ]);
37
38        $componentsBuilder->setSchema($componentIdentifier, $schema);
39
40        return $componentsBuilder->getComponents();
41    }
42
43    public function addCreationSchemaFor(ComponentsBuilder $componentsBuilder, string $componentIdentifier, ReflectionClass $class, bool $nullable = false): Components
44    {
45        $alias = ApieLib::getAlias($class->name);
46        $type = ReflectionTypeFactory::createReflectionType($alias);
47        $types = $type instanceof \ReflectionUnionType ? $type->getTypes() : [$type];
48        $oneOfs = [];
49        foreach ($types as $type) {
50            $oneOfs[] = $componentsBuilder->getSchemaForType($type, false, false, $nullable);
51        }
52        $schema =  new Schema([
53            'oneOf' => $oneOfs
54        ]);
55
56        $componentsBuilder->setSchema($componentIdentifier, $schema);
57
58        return $componentsBuilder->getComponents();
59    }
60
61    public function addModificationSchemaFor(
62        ComponentsBuilder $componentsBuilder,
63        string $componentIdentifier,
64        ReflectionClass $class
65    ): Components {
66        $alias = ApieLib::getAlias($class->name);
67        $type = ReflectionTypeFactory::createReflectionType($alias);
68        $types = $type instanceof \ReflectionUnionType ? $type->getTypes() : [$type];
69        $oneOfs = [];
70        foreach ($types as $type) {
71            $oneOfs[] = $componentsBuilder->getSchemaForType($type, false, false, $type->allowsNull());
72        }
73        $schema =  new Schema([
74            'oneOf' => $oneOfs
75        ]);
76
77        $componentsBuilder->setSchema($componentIdentifier, $schema);
78
79        return $componentsBuilder->getComponents();
80    }
81}