1 | <?php |
||
2 | |||
3 | |||
4 | namespace Apie\OpenapiSchema\Spec; |
||
5 | |||
6 | use Apie\CompositeValueObjects\ValueObjects\StringList; |
||
7 | use Apie\OpenapiSchema\Concerns\CompositeValueObjectWithExtension; |
||
8 | use Apie\OpenapiSchema\Exceptions\DefaultValueNotInEnum; |
||
9 | use Apie\OpenapiSchema\ValueObjects\SpecificationExtension; |
||
10 | use Apie\ValueObjects\ValueObjectInterface; |
||
11 | |||
12 | /** |
||
13 | * @see https://swagger.io/specification/#server-variable-object |
||
14 | */ |
||
15 | class ServerVariable implements ValueObjectInterface |
||
16 | { |
||
17 | use CompositeValueObjectWithExtension; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $default; |
||
23 | |||
24 | /** |
||
25 | * @var string|null |
||
26 | */ |
||
27 | private $description; |
||
28 | |||
29 | /** |
||
30 | * @var StringList|null |
||
31 | */ |
||
32 | private $enums = null; |
||
33 | |||
34 | public function __construct(string $default, ?string $description, string... $enums) |
||
35 | { |
||
36 | $this->default = $default; |
||
37 | $this->description = $description; |
||
38 | if (!empty($enums)) { |
||
39 | $this->enums = StringList::fromNative($enums); |
||
40 | } |
||
41 | $this->validateProperties(); |
||
42 | $this->specificationExtension = new SpecificationExtension([]); |
||
43 | } |
||
44 | |||
45 | protected function validateProperties(): void |
||
46 | { |
||
47 | if (!empty($this->enums) && !in_array($this->default, $this->enums->toNative(), true)) { |
||
48 | throw new DefaultValueNotInEnum($this->default, ...$this->enums); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | public function getDefault(): string |
||
53 | { |
||
54 | return $this->default; |
||
55 | } |
||
56 | |||
57 | public function getDescription(): ?string |
||
58 | { |
||
59 | return $this->description; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return string[]|null |
||
64 | */ |
||
65 | public function getEnums(): ?array |
||
66 | { |
||
67 | return $this->enums ? $this->enums->toNative() : null; |
||
68 | } |
||
69 | } |
||
70 |