1
|
|
|
<?php |
2
|
|
|
namespace gossi\swagger\collections; |
3
|
|
|
|
4
|
|
|
use gossi\swagger\parts\ExtensionPart; |
5
|
|
|
use gossi\swagger\Response; |
6
|
|
|
use phootwork\collection\CollectionUtils; |
7
|
|
|
use phootwork\collection\Map; |
8
|
|
|
use phootwork\lang\Arrayable; |
9
|
|
|
use phootwork\lang\Text; |
10
|
|
|
|
11
|
|
View Code Duplication |
class Responses implements Arrayable { |
|
|
|
|
12
|
|
|
|
13
|
|
|
use ExtensionPart; |
14
|
|
|
|
15
|
|
|
/** @var Map */ |
16
|
|
|
private $responses; |
17
|
|
|
|
18
|
11 |
|
public function __construct($contents = null) { |
19
|
11 |
|
$this->parse($contents === null ? new Map() : $contents); |
20
|
11 |
|
} |
21
|
|
|
|
22
|
11 |
|
private function parse($contents) { |
23
|
11 |
|
$data = CollectionUtils::toMap($contents); |
24
|
|
|
|
25
|
|
|
// responses |
26
|
11 |
|
$this->responses = new Map(); |
27
|
11 |
|
foreach ($data as $r => $response) { |
28
|
5 |
|
if (!Text::create($r)->startsWith('x-')) { |
29
|
5 |
|
$this->responses->set($r, new Response($r, $response)); |
30
|
5 |
|
} |
31
|
11 |
|
} |
32
|
|
|
|
33
|
|
|
// extensions |
34
|
11 |
|
$this->parseExtensions($data); |
35
|
11 |
|
} |
36
|
|
|
|
37
|
6 |
|
public function toArray() { |
38
|
6 |
|
$responses = clone $this->responses; |
39
|
6 |
|
$responses->setAll($this->getExtensions()); |
40
|
6 |
|
return $responses->toArray(); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function size() { |
44
|
1 |
|
return $this->responses->size(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns whether the given response exists |
49
|
|
|
* |
50
|
|
|
* @param string $code |
51
|
|
|
* @return boolean |
52
|
|
|
*/ |
53
|
1 |
|
public function has($code) { |
54
|
1 |
|
return $this->responses->has($code); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns whether the given response exists |
59
|
|
|
* |
60
|
|
|
* @param Response $response |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
1 |
|
public function contains(Response $response) { |
64
|
1 |
|
return $this->responses->contains($response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the reponse info for the given code |
69
|
|
|
* |
70
|
|
|
* @param string $code |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
2 |
|
public function get($code) { |
74
|
2 |
|
return $this->responses->get($code); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the response |
79
|
|
|
* |
80
|
|
|
* @param Response $code |
|
|
|
|
81
|
|
|
*/ |
82
|
1 |
|
public function add(Response $response) { |
83
|
1 |
|
$this->responses->set($response->getCode(), $response); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Removes the given repsonse |
88
|
|
|
* |
89
|
|
|
* @param string $code |
90
|
|
|
*/ |
91
|
1 |
|
public function remove($code) { |
92
|
1 |
|
$this->responses->remove($code); |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|
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.