|
1
|
|
|
<?php |
|
2
|
|
|
namespace gossi\swagger; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\swagger\parts\ExtensionPart; |
|
5
|
|
|
use phootwork\collection\CollectionUtils; |
|
6
|
|
|
use phootwork\collection\Map; |
|
7
|
|
|
use phootwork\lang\Arrayable; |
|
8
|
|
|
|
|
9
|
|
|
class Path extends AbstractModel implements Arrayable { |
|
10
|
|
|
|
|
11
|
|
|
use ExtensionPart; |
|
12
|
|
|
|
|
13
|
|
|
private $operations; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $path; |
|
17
|
|
|
|
|
18
|
8 |
|
public function __construct($path, $contents = []) { |
|
19
|
8 |
|
$this->path = $path; |
|
20
|
8 |
|
$this->operations = new Map(); |
|
21
|
8 |
|
$this->parse($contents); |
|
22
|
8 |
|
} |
|
23
|
|
|
|
|
24
|
8 |
|
private function parse($contents) { |
|
25
|
8 |
|
$data = CollectionUtils::toMap($contents); |
|
26
|
|
|
|
|
27
|
8 |
|
foreach (Swagger::$METHODS as $method) { |
|
28
|
8 |
|
if ($data->has($method)) { |
|
29
|
6 |
|
$this->operations->set($method, new Operation($data->get($method))); |
|
30
|
6 |
|
} |
|
31
|
8 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
// parts |
|
34
|
8 |
|
$this->parseExtensions($data); |
|
35
|
8 |
|
} |
|
36
|
|
|
|
|
37
|
7 |
|
public function toArray() { |
|
38
|
7 |
|
return array_merge( |
|
39
|
7 |
|
CollectionUtils::toArrayRecursive($this->operations), |
|
40
|
7 |
|
CollectionUtils::toArrayRecursive($this->getExtensions()) |
|
41
|
7 |
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns this path |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function getPath() { |
|
50
|
1 |
|
return $this->path; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Gets the operation for the given method, creates one if none exists |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $method |
|
57
|
|
|
* @return Operation |
|
58
|
|
|
*/ |
|
59
|
7 |
|
public function getOperation($method) { |
|
60
|
7 |
|
if (!$this->operations->has($method)) { |
|
61
|
1 |
|
$this->operations->set($method, new Operation()); |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
return $this->operations->get($method); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sets the operation for a method |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $method |
|
71
|
|
|
* @param Operation $operation |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setOperation($method, Operation $operation) { |
|
74
|
|
|
$this->operations->set($method, $operation); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $method |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function hasOperation($method) { |
|
83
|
|
|
return $this->operations->has($method); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Removes an operation for the given method |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $method |
|
90
|
|
|
*/ |
|
91
|
|
|
public function removeOperation($method) { |
|
92
|
|
|
$this->operations->remove($method); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns all methods for this path |
|
97
|
|
|
* |
|
98
|
|
|
* @return Set |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getMethods() { |
|
101
|
|
|
return $this->operations->keys(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|