1 | <?php |
||
2 | |||
3 | |||
4 | namespace Apie\OpenapiSchema\Spec; |
||
5 | |||
6 | use Apie\OpenapiSchema\Concerns\CompositeValueObjectWithExtension; |
||
7 | use Apie\OpenapiSchema\Exceptions\MissingPlaceholderVariables; |
||
8 | use Apie\OpenapiSchema\Map\ServerVariableObjectList; |
||
9 | use Apie\OpenapiSchema\ValueObjects\UrlsWithPlaceholders; |
||
10 | use Apie\ValueObjects\ValueObjectInterface; |
||
11 | |||
12 | /** |
||
13 | * @see https://swagger.io/specification/#server-object |
||
14 | */ |
||
15 | class Server implements ValueObjectInterface |
||
16 | { |
||
17 | use CompositeValueObjectWithExtension; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
18 | |||
19 | /** |
||
20 | * @var UrlsWithPlaceholders |
||
21 | */ |
||
22 | private $url; |
||
23 | |||
24 | /** |
||
25 | * @var string|null |
||
26 | */ |
||
27 | private $description; |
||
28 | |||
29 | /** |
||
30 | * @var ServerVariableObjectList|null |
||
31 | */ |
||
32 | private $variables; |
||
33 | |||
34 | public function __construct(UrlsWithPlaceholders $url) |
||
35 | { |
||
36 | $this->url = $url; |
||
37 | $this->validateProperties(); |
||
38 | } |
||
39 | |||
40 | public function getUrl(): UrlsWithPlaceholders |
||
41 | { |
||
42 | return $this->url; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return string|null |
||
47 | */ |
||
48 | public function getDescription(): ?string |
||
49 | { |
||
50 | return $this->description; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return ServerVariableObjectList|null |
||
55 | */ |
||
56 | public function getVariables(): ?ServerVariableObjectList |
||
57 | { |
||
58 | return $this->variables; |
||
59 | } |
||
60 | |||
61 | protected function validateProperties(): void |
||
62 | { |
||
63 | $placeholders = $this->url->getPlaceholders(); |
||
64 | if (!$this->variables && $placeholders) { |
||
65 | throw new MissingPlaceholderVariables($placeholders); |
||
66 | } |
||
67 | $missing = []; |
||
68 | foreach ($placeholders as $placeholder) { |
||
69 | if (!isset($this->variables[$placeholder])) { |
||
70 | $missing[] = $placeholder; |
||
71 | } |
||
72 | } |
||
73 | if (!empty($missing)) { |
||
74 | throw new MissingPlaceholderVariables($missing); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 |