1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Document; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification\Operation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author John Kleijn <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Specification |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var object |
20
|
|
|
*/ |
21
|
|
|
private $definition; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Operation[] |
25
|
|
|
*/ |
26
|
|
|
private $operations; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \stdClass $definition |
30
|
|
|
*/ |
31
|
|
|
public function __construct(\stdClass $definition) |
32
|
|
|
{ |
33
|
|
|
$this->definition = $definition; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param callable $f |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function apply(callable $f) |
42
|
|
|
{ |
43
|
|
|
$recurse = function (&$item) use ($f, &$recurse) { |
44
|
|
|
|
45
|
|
|
foreach ($item as $attribute => &$value) { |
46
|
|
|
if (false === $f($value, $attribute)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
if ($value === null) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
if (!is_scalar($value)) { |
53
|
|
|
if (false === $recurse($value)) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
}; |
61
|
|
|
$recurse($this->definition); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return \stdClass |
66
|
|
|
*/ |
67
|
|
|
public function getDefinition(): \stdClass |
68
|
|
|
{ |
69
|
|
|
return $this->definition; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \stdClass |
74
|
|
|
*/ |
75
|
|
|
public function getPaths(): \stdClass |
76
|
|
|
{ |
77
|
|
|
return $this->definition->paths; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $path |
82
|
|
|
* @param string $method |
83
|
|
|
* |
84
|
|
|
* @return Operation |
85
|
|
|
*/ |
86
|
|
|
public function getOperation(string $path, string $method): Operation |
87
|
|
|
{ |
88
|
|
|
$operation = new Operation($this, $path, $method); |
89
|
|
|
|
90
|
|
|
if (isset($this->operations[$operation->getId()])) { |
91
|
|
|
return $this->operations[$operation->getId()]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->operations[$operation->getId()] = $operation; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Operation[] |
99
|
|
|
*/ |
100
|
|
|
public function getOperations(): array |
101
|
|
|
{ |
102
|
|
|
$operations = []; |
103
|
|
|
|
104
|
|
|
foreach ($this->getPaths() as $path => $pathItem) { |
|
|
|
|
105
|
|
|
foreach (array_keys((array)$pathItem) as $method) { |
106
|
|
|
$operations[] = $this->getOperation($path, $method); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $operations; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @deprecated |
115
|
|
|
* |
116
|
|
|
* @param string $path |
117
|
|
|
* @param string $method |
118
|
|
|
* |
119
|
|
|
* @return \stdClass |
120
|
|
|
*/ |
121
|
|
|
public function getOperationDefinition(string $path, string $method): \stdClass |
122
|
|
|
{ |
123
|
|
|
return $this->getOperation($path, $method)->getDefinition(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|