1
|
|
|
<?php |
2
|
|
|
namespace gossi\swagger\collections; |
3
|
|
|
|
4
|
|
|
use gossi\swagger\parts\RefPart; |
5
|
|
|
use phootwork\collection\ArrayList; |
6
|
|
|
use phootwork\collection\CollectionUtils; |
7
|
|
|
use phootwork\lang\Arrayable; |
8
|
|
|
use gossi\swagger\Parameter; |
9
|
|
|
|
10
|
|
|
class Parameters implements Arrayable, \Iterator { |
11
|
|
|
|
12
|
|
|
use RefPart; |
13
|
|
|
|
14
|
|
|
/** @var ArrayList */ |
15
|
|
|
private $parameters; |
16
|
|
|
|
17
|
12 |
|
public function __construct($contents = []) { |
18
|
12 |
|
$this->parse($contents === null ? [] : $contents); |
19
|
12 |
|
} |
20
|
|
|
|
21
|
12 |
|
private function parse($contents) { |
22
|
12 |
|
$data = CollectionUtils::toMap($contents); |
23
|
|
|
|
24
|
12 |
|
$this->parameters = new ArrayList(); |
25
|
12 |
|
$this->parseRef($data); |
26
|
|
|
|
27
|
12 |
|
if (!$this->hasRef()) { |
28
|
12 |
|
foreach ($data as $param) { |
29
|
4 |
|
$this->parameters->add(new Parameter($param)); |
30
|
12 |
|
} |
31
|
12 |
|
} |
32
|
12 |
|
} |
33
|
|
|
|
34
|
8 |
|
public function toArray() { |
35
|
8 |
|
if ($this->hasRef()) { |
36
|
1 |
|
return ['$ref' => $this->getRef()]; |
37
|
|
|
} |
38
|
|
|
|
39
|
7 |
|
return $this->parameters->toArray(); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function size() { |
43
|
1 |
|
return $this->parameters->size(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Searches whether a parameter with the given name exists |
48
|
|
|
* |
49
|
|
|
* @param string $name |
50
|
|
|
* @return boolean |
51
|
|
|
*/ |
52
|
1 |
|
public function searchByName($name) { |
53
|
|
|
return $this->parameters->search($name, function(Parameter $param, $name) { |
54
|
1 |
|
return $param->getName() == $name; |
55
|
1 |
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns parameter with the given name if it exists |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* @return Parameter|void |
63
|
|
|
*/ |
64
|
12 |
|
public function findByName($name) { |
65
|
1 |
|
foreach ($this->parameters as $param) { |
66
|
1 |
|
if ($param->getName() == $name) { |
67
|
1 |
|
return $param; |
68
|
|
|
} |
69
|
12 |
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Searches for the parameter with the given name. Creates a parameter with the given name |
74
|
|
|
* if none exists |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @return Parameter |
78
|
|
|
*/ |
79
|
|
|
public function getByName($name) { |
80
|
|
|
$param = $this->findByName($name); |
81
|
|
|
if (empty($param)) { |
82
|
|
|
$param = new Parameter(); |
83
|
|
|
$param->setName($name); |
84
|
|
|
$this->parameters->add($param); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $param; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Searches whether a parameter with the given unique combination exists |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* @param string $id |
|
|
|
|
95
|
|
|
* @return boolean |
96
|
|
|
*/ |
97
|
|
|
public function search($name, $in) { |
98
|
1 |
|
return $this->parameters->search(function(Parameter $param) use ($name, $in) { |
99
|
1 |
|
return $param->getIn() == $in && $param->getName() == $name; |
100
|
1 |
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
public function find($name, $in) { |
104
|
1 |
|
foreach ($this->parameters as $param) { |
105
|
1 |
|
if ($param->getIn() == $in && $param->getName() == $name) { |
106
|
1 |
|
return $param; |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
public function get($name, $in) { |
112
|
1 |
|
$param = $this->find($name, $in); |
113
|
1 |
|
if (empty($param)) { |
114
|
1 |
|
$param = new Parameter(); |
115
|
1 |
|
$param->setName($name); |
116
|
1 |
|
$param->setIn($in); |
117
|
1 |
|
$this->parameters->add($param); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
return $param; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds a parameter |
125
|
|
|
* |
126
|
|
|
* @param Parameter $param |
127
|
|
|
*/ |
128
|
1 |
|
public function add(Parameter $param) { |
129
|
1 |
|
$this->parameters->add($param); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Removes a parameter |
134
|
|
|
* |
135
|
|
|
* @param Parameter $param |
136
|
|
|
*/ |
137
|
1 |
|
public function remove(Parameter $param) { |
138
|
1 |
|
$this->parameters->remove($param); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns whether a given parameter exists |
143
|
|
|
* |
144
|
|
|
* @param Parameter $param |
145
|
|
|
* @return boolean |
146
|
|
|
*/ |
147
|
1 |
|
public function contains(Parameter $param) { |
148
|
1 |
|
return $this->parameters->contains($param); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function current() { |
152
|
|
|
return $this->parameters->current(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function key() { |
156
|
|
|
return $this->parameters->key(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function next() { |
160
|
|
|
return $this->parameters->next(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function rewind() { |
164
|
|
|
return $this->parameters->rewind(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function valid() { |
168
|
|
|
return $this->parameters->valid(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.