Passed
Push — main ( ef112d...d39948 )
by Pieter
03:42
created

ServerVariable::fromNative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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