Completed
Push — master ( 48b842...eabf80 )
by Maxime
01:29
created

ElaoEnumType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getType() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\ApiPlatform\Core\JsonSchema\Type;
12
13
use ApiPlatform\Core\JsonSchema\Schema;
14
use ApiPlatform\Core\JsonSchema\TypeFactoryInterface;
15
use Elao\Enum\EnumInterface;
16
use Symfony\Component\PropertyInfo\Type;
17
18
final class ElaoEnumType implements TypeFactoryInterface
19
{
20
    /** @var TypeFactoryInterface */
21
    private $decorated;
22
23
    public function __construct(TypeFactoryInterface $decorated)
24
    {
25
        $this->decorated = $decorated;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array
32
    {
33
        if (!is_a($enumClass = $type->getClassName(), EnumInterface::class, true)) {
34
            return $this->decorated->getType($type, $format, $readableLink, $serializerContext, $schema);
35
        }
36
37
        $values = $enumClass::values();
38
39
        return [
40
            'type' => 'string',
41
            'enum' => $values,
42
            'example' => $values[0],
43
        ];
44
    }
45
}
46