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) |
|
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 = []) |
|
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) |
|
93 | { |
||
94 | /** |
||
95 | * @var array $swagger |
||
96 | */ |
||
97 | 4 | $swagger = $this->decodeJson($input, true); |
|
98 | |||
99 | 4 | if (empty($swagger)) { |
|
100 | return false; |
||
101 | } |
||
102 | |||
103 | 4 | $mandatoryFields = ['swagger', 'info', 'paths', 'version', 'title', 'definitions']; |
|
104 | 4 | $fields = array_merge(array_keys($swagger), array_keys($swagger['info'])); |
|
105 | 4 | $intersect = array_intersect($mandatoryFields, $fields); |
|
106 | |||
107 | // every mandatory field was found in provided json definition. |
||
108 | 4 | return empty(array_diff($mandatoryFields, $intersect)); |
|
109 | } |
||
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 |