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