ArrayPhpTypeSchemaResolver::supportsPhpType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\OpenApi\PhpTypeSchemaResolver;
15
16
use ReflectionAttribute;
17
use ReflectionParameter;
18
use ReflectionProperty;
19
use Reflector;
20
use Sunrise\Http\Router\OpenApi\Exception\UnsupportedPhpTypeException;
21
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverInterface;
22
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface;
23
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface;
24
use Sunrise\Http\Router\OpenApi\Type;
25
use Sunrise\Hydrator\Annotation\Subtype;
26
27
/**
28
 * @since 3.0.0
29
 */
30
final class ArrayPhpTypeSchemaResolver implements
31
    OpenApiPhpTypeSchemaResolverInterface,
32
    OpenApiPhpTypeSchemaResolverManagerAwareInterface
33
{
34
    private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager;
35
36 5
    public function setOpenApiPhpTypeSchemaResolverManager(
37
        OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager,
38
    ): void {
39 5
        $this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager;
0 ignored issues
show
Bug introduced by
The property openApiPhpTypeSchemaResolverManager is declared read-only in Sunrise\Http\Router\Open...ayPhpTypeSchemaResolver.
Loading history...
40
    }
41
42 3
    public function supportsPhpType(Type $phpType, Reflector $phpTypeHolder): bool
43
    {
44 3
        return $phpType->name == Type::PHP_TYPE_NAME_ARRAY;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 3
    public function resolvePhpTypeSchema(Type $phpType, Reflector $phpTypeHolder): array
51
    {
52 3
        $this->supportsPhpType($phpType, $phpTypeHolder) or throw new UnsupportedPhpTypeException();
53
54 3
        $phpTypeSchema = [
55 3
            'oneOf' => [
56 3
                [
57 3
                    'type' => Type::OAS_TYPE_NAME_ARRAY,
58 3
                ],
59 3
                [
60 3
                    'type' => Type::OAS_TYPE_NAME_OBJECT,
61 3
                ],
62 3
            ],
63 3
        ];
64
65
        if (
66 3
            $phpTypeHolder instanceof ReflectionParameter ||
67 3
            $phpTypeHolder instanceof ReflectionProperty
68
        ) {
69
            /** @var list<ReflectionAttribute<Subtype>> $annotations */
70 3
            $annotations = $phpTypeHolder->getAttributes(Subtype::class);
71 3
            if (isset($annotations[0])) {
72 3
                $annotation = $annotations[0]->newInstance();
73
74 3
                $arrayElementPhpType = new Type($annotation->name, $annotation->allowsNull);
75 3
                $arrayElementPhpTypeSchema = $this->openApiPhpTypeSchemaResolverManager
76 3
                    ->resolvePhpTypeSchema($arrayElementPhpType, $phpTypeHolder);
77
78 3
                $phpTypeSchema['oneOf'][0]['items'] = $arrayElementPhpTypeSchema;
79 3
                $phpTypeSchema['oneOf'][1]['additionalProperties'] = $arrayElementPhpTypeSchema;
80
81 3
                if ($annotation->limit !== null) {
82 3
                    $phpTypeSchema['oneOf'][0]['maxItems'] = $annotation->limit;
83 3
                    $phpTypeSchema['oneOf'][1]['maxProperties'] = $annotation->limit;
84
                }
85
            }
86
        }
87
88 3
        return $phpTypeSchema;
89
    }
90
91 3
    public function getWeight(): int
92
    {
93 3
        return 0;
94
    }
95
}
96