1 | <?php |
||
23 | class SwaggerStrategy implements DispersalStrategyInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $fallbackData = []; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | */ |
||
33 | private $document; |
||
34 | |||
35 | /** |
||
36 | * constructor |
||
37 | * |
||
38 | * @param Document $document Swagger document parser |
||
39 | */ |
||
40 | 8 | public function __construct(Document $document) |
|
41 | { |
||
42 | 8 | $this->document = $document; |
|
43 | 8 | } |
|
44 | |||
45 | /** |
||
46 | * process data |
||
47 | * |
||
48 | * @param string $input JSON information about the swagger service. |
||
49 | * @param array $fallbackData Set of information to be registered in case the swagger info is not complete. |
||
50 | * |
||
51 | * @return ApiDefinition |
||
52 | */ |
||
53 | 4 | public function process($input, array $fallbackData = []) |
|
54 | { |
||
55 | 4 | $this->registerFallbackData($fallbackData); |
|
56 | 2 | $apiDef = new ApiDefinition(); |
|
57 | |||
58 | /** |
||
59 | * @var \stdClass $swagger |
||
60 | */ |
||
61 | 2 | $swagger = $this->decodeJson($input); |
|
62 | 2 | if (is_object($swagger)) { |
|
63 | 2 | $this->document->setDocument($swagger); |
|
64 | 2 | $this->setBaseValues($apiDef); |
|
65 | |||
66 | 2 | $operations = $this->document->getOperationsById(); |
|
67 | 2 | foreach ($operations as $service) { |
|
68 | 2 | $path = $service->getPath(); |
|
69 | |||
70 | 2 | if (in_array(strtolower($service->getMethod()), ['delete', 'patch']) || $apiDef->hasEndpoint($path)) { |
|
71 | 2 | continue; |
|
72 | } |
||
73 | 2 | $apiDef->addEndpoint($path); |
|
74 | 2 | $apiDef->addSchema( |
|
75 | 1 | $path, |
|
76 | 2 | $this->getServiceSchema($service) |
|
77 | 1 | ); |
|
78 | 2 | $apiDef->setOrigin($this->document); |
|
79 | 1 | } |
|
80 | 1 | } |
|
81 | |||
82 | 2 | return $apiDef; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * is input data valid json |
||
87 | * |
||
88 | * @param string $input json string |
||
89 | * |
||
90 | * @return boolean |
||
91 | */ |
||
92 | 4 | public function supports($input) |
|
110 | |||
111 | /** |
||
112 | * decode a json string |
||
113 | * |
||
114 | * @param string $input json string |
||
115 | * @param bool $assoc Force the encoded result to be a hash. |
||
116 | * |
||
117 | * @return array|\stdClass |
||
118 | */ |
||
119 | 6 | private function decodeJson($input, $assoc = false) |
|
125 | |||
126 | /** |
||
127 | * set base values |
||
128 | * |
||
129 | * @param ApiDefinition $apiDef API definition |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | 2 | private function setBaseValues(ApiDefinition $apiDef) |
|
141 | |||
142 | /** |
||
143 | * get the schema |
||
144 | * |
||
145 | * @param OperationReference $service service endpoint |
||
146 | * |
||
147 | * @return \stdClass |
||
148 | */ |
||
149 | 2 | private function getServiceSchema($service) |
|
188 | |||
189 | /** |
||
190 | * resolve schema |
||
191 | * |
||
192 | * @param \stdClass $reference reference |
||
193 | * |
||
194 | * @return \stdClass |
||
195 | */ |
||
196 | 2 | private function resolveSchema($reference) |
|
225 | |||
226 | /** |
||
227 | * Sets the destination host for the api definition. |
||
228 | * |
||
229 | * @param ApiDefinition $apiDef Configuration for the swagger api to be recognized. |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | 2 | private function registerHost(ApiDefinition $apiDef) |
|
242 | |||
243 | /** |
||
244 | * Set of information to be used as default if not defined by the swagger configuration. |
||
245 | * |
||
246 | * @param array $fallbackData Set of default information (e.g. host) |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | 4 | private function registerFallbackData(array $fallbackData) |
|
258 | } |
||
259 |