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
|
6 |
|
public function toArray() { |
38
|
6 |
|
return array_merge($this->operations->toArray(), $this->getExtensions()->toArray()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns this path |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
1 |
|
public function getPath() { |
47
|
1 |
|
return $this->path; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the operation for the given method, creates one if none exists |
52
|
|
|
* |
53
|
|
|
* @param string $method |
54
|
|
|
* @return Operation |
55
|
|
|
*/ |
56
|
2 |
|
public function getOperation($method) { |
57
|
2 |
|
if (!$this->operations->has($method)) { |
58
|
1 |
|
$this->operations->set($method, new Operation()); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
2 |
|
return $this->operations->get($method); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @param string $method |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
|
|
public function hasOperation($method) { |
70
|
|
|
return $this->operations->has($method); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Removes an operation for the given method |
75
|
|
|
* |
76
|
|
|
* @param string $method |
77
|
|
|
*/ |
78
|
|
|
public function removeOperation($method) { |
79
|
|
|
$this->operations->remove($method); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |