SymfonyUidPhpTypeSchemaResolver::getWeight()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
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 Reflector;
17
use Sunrise\Http\Router\OpenApi\Exception\UnsupportedPhpTypeException;
18
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverInterface;
19
use Sunrise\Http\Router\OpenApi\Type;
20
use Symfony\Component\Uid\AbstractUid;
21
use Symfony\Component\Uid\Uuid;
22
23
use function is_a;
24
use function is_subclass_of;
25
26
/**
27
 * @since 3.0.0
28
 */
29
final class SymfonyUidPhpTypeSchemaResolver implements OpenApiPhpTypeSchemaResolverInterface
30
{
31 3
    public function supportsPhpType(Type $phpType, Reflector $phpTypeHolder): bool
32
    {
33 3
        return is_subclass_of($phpType->name, AbstractUid::class);
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 3
    public function resolvePhpTypeSchema(Type $phpType, Reflector $phpTypeHolder): array
40
    {
41 3
        $this->supportsPhpType($phpType, $phpTypeHolder) or throw new UnsupportedPhpTypeException();
42
43 3
        $phpTypeSchema = [
44 3
            'type' => Type::OAS_TYPE_NAME_STRING,
45 3
        ];
46
47 3
        if (is_a($phpType->name, Uuid::class, true)) {
48 3
            $phpTypeSchema['format'] = 'uuid';
49
        }
50
51 3
        return $phpTypeSchema;
52
    }
53
54 3
    public function getWeight(): int
55
    {
56 3
        return 0;
57
    }
58
}
59