1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Description; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author John Kleijn <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Path implements Element |
17
|
|
|
{ |
18
|
|
|
use VisiteeMixin; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $path; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Operation[] |
27
|
|
|
*/ |
28
|
|
|
protected $operations = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Parameter[] |
32
|
|
|
*/ |
33
|
|
|
protected $pathParameters = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $extensions; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Path constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $path |
44
|
|
|
* @param Operation[] $operations |
45
|
|
|
* @param Parameter[] $pathParameters |
46
|
|
|
* @param array $extensions |
47
|
|
|
*/ |
48
|
|
|
public function __construct($path, array $operations, array $pathParameters = [], array $extensions = []) |
49
|
|
|
{ |
50
|
|
|
$this->path = $path; |
51
|
|
|
$this->operations = $operations; |
52
|
|
|
$this->pathParameters = $pathParameters; |
53
|
|
|
$this->extensions = $extensions; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getExtension(string $name) |
62
|
|
|
{ |
63
|
|
|
return isset($this->extensions[$name]) ? $this->extensions[$name] : null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getPath(): string |
70
|
|
|
{ |
71
|
|
|
return $this->path; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Operation[] |
76
|
|
|
*/ |
77
|
|
|
public function getOperations(): array |
78
|
|
|
{ |
79
|
|
|
return $this->operations; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $method |
84
|
|
|
* |
85
|
|
|
* @return Operation |
86
|
|
|
*/ |
87
|
|
|
public function getOperation(string $method): Operation |
88
|
|
|
{ |
89
|
|
|
if (!$this->hasOperation($method)) { |
90
|
|
|
throw new \InvalidArgumentException( |
91
|
|
|
"Path '{$this->getPath()}' does not support '$method'" . |
92
|
|
|
" (supports " . implode(', ', array_keys($this->operations)) . ')' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->operations[strtolower($method)]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $method |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function hasOperation(string $method): bool |
104
|
|
|
{ |
105
|
|
|
return isset($this->operations[strtolower($method)]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Parameter[] |
110
|
|
|
*/ |
111
|
|
|
public function getPathParameters(): array |
112
|
|
|
{ |
113
|
|
|
return $this->pathParameters; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|