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
|
|
|
$method = strtolower($method); |
90
|
|
|
|
91
|
|
View Code Duplication |
if (!isset($this->operations[$method])) { |
|
|
|
|
92
|
|
|
throw new \InvalidArgumentException( |
93
|
|
|
"Path '{$this->getPath()}' does not support '$method'" . |
94
|
|
|
" (supports " . implode(', ', array_keys($this->operations)) . ')' |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->operations[$method]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return Parameter[] |
103
|
|
|
*/ |
104
|
|
|
public function getPathParameters(): array |
105
|
|
|
{ |
106
|
|
|
return $this->pathParameters; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.