|
84 | 84 | $data |
85 | 85 | ['paths'] |
86 | 86 | ['/ocs/v2.php/cloud/capabilities'] |
87 | | -['get'] |
88 | | -['responses'] |
89 | | -['200'] |
90 | | -['content'] |
91 | | -['application/json'] |
92 | | -['schema'] |
93 | | -['properties'] |
94 | | -['ocs'] |
95 | | -['properties'] |
96 | | -['data'] |
97 | | -['properties'] |
98 | | -['capabilities'] |
99 | | -['anyOf'] |
100 | | - = array_map(fn (string $capability): array => ['$ref' => '#/components/schemas/' . $capability], $capabilities); |
101 | | - |
102 | | -function loadSpec(string $path): array { |
103 | | - return rewriteRefs(json_decode(file_get_contents($path), true)); |
| 87 | + ->get |
| 88 | + ->responses |
| 89 | + ->{'200'} |
| 90 | + ->content |
| 91 | + ->{'application/json'} |
| 92 | + ->schema |
| 93 | + ->properties |
| 94 | + ->ocs |
| 95 | + ->properties |
| 96 | + ->data |
| 97 | + ->properties |
| 98 | + ->capabilities |
| 99 | + ->anyOf |
| 100 | + = array_map(static fn (string $capability): array => ['$ref' => '#/components/schemas/' . $capability], $capabilities); |
| 101 | + |
| 102 | +function loadSpec(string $path): object { |
| 103 | + return rewriteRefs(json_decode(file_get_contents($path), false, 512, JSON_THROW_ON_ERROR)); |
104 | 104 | } |
105 | 105 |
|
106 | | -function rewriteRefs(array $spec): array { |
107 | | - $readableAppID = Helpers::generateReadableAppID($spec['info']['title']); |
108 | | - array_walk_recursive($spec, function (mixed &$item, string $key) use ($readableAppID): void { |
| 106 | +function rewriteRefs(object $spec): object { |
| 107 | + $readableAppID = Helpers::generateReadableAppID($spec->info->title); |
| 108 | + object_walk_recursive($spec, static function (mixed &$item, string $key) use ($readableAppID): void { |
109 | 109 | if ($key === '$ref' && $item !== '#/components/schemas/OCSMeta') { |
110 | 110 | $item = str_replace('#/components/schemas/', '#/components/schemas/' . $readableAppID, $item); |
111 | 111 | } |
112 | 112 | }); |
113 | 113 | return $spec; |
114 | 114 | } |
115 | 115 |
|
116 | | -function collectCapabilities(array $spec): array { |
| 116 | +function object_walk_recursive(object $object, \Closure $callback): void { |
| 117 | + foreach (array_keys(get_object_vars($object)) as $key) { |
| 118 | + $callback($object->{$key}, $key); |
| 119 | + if (is_object($object->{$key})) { |
| 120 | + object_walk_recursive($object->{$key}, $callback); |
| 121 | + } |
| 122 | + if (is_array($object->{$key})) { |
| 123 | + foreach ($object->{$key} as $item) { |
| 124 | + if (is_object($item)) { |
| 125 | + object_walk_recursive($item, $callback); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +function collectCapabilities(object $spec): array { |
117 | 133 | $capabilities = []; |
118 | | - $readableAppID = Helpers::generateReadableAppID($spec['info']['title']); |
119 | | - foreach (array_keys($spec['components']['schemas']) as $name) { |
120 | | - if ($name == 'Capabilities' || $name == 'PublicCapabilities') { |
| 134 | + $readableAppID = Helpers::generateReadableAppID($spec->info->title); |
| 135 | + foreach (array_keys(get_object_vars($spec->components->schemas)) as $name) { |
| 136 | + if ($name === 'Capabilities' || $name === 'PublicCapabilities') { |
121 | 137 | $capabilities[] = $readableAppID . $name; |
122 | 138 | } |
123 | 139 | } |
124 | 140 |
|
125 | 141 | return $capabilities; |
126 | 142 | } |
127 | 143 |
|
128 | | -function rewriteSchemaNames(array $spec): array { |
129 | | - $schemas = $spec['components']['schemas']; |
130 | | - $readableAppID = Helpers::generateReadableAppID($spec['info']['title']); |
| 144 | +function rewriteSchemaNames(object $spec): array { |
| 145 | + $schemas = get_object_vars($spec->components->schemas); |
| 146 | + $readableAppID = Helpers::generateReadableAppID($spec->info->title); |
131 | 147 | return array_combine( |
132 | | - array_map(fn (string $key): string => $key === 'OCSMeta' ? $key : $readableAppID . $key, array_keys($schemas)), |
| 148 | + array_map(static fn (string $key): string => $key === 'OCSMeta' ? $key : $readableAppID . $key, array_keys($schemas)), |
133 | 149 | array_values($schemas), |
134 | 150 | ); |
135 | 151 | } |
136 | 152 |
|
137 | | -function rewriteTags(array $spec): array { |
138 | | - return array_map(function (array $tag) use ($spec) { |
139 | | - $tag['name'] = $spec['info']['title'] . '/' . $tag['name']; |
| 153 | +function rewriteTags(object $spec): array { |
| 154 | + return array_map(static function (object $tag) use ($spec) { |
| 155 | + $tag->name = $spec->info->title . '/' . $tag->name; |
140 | 156 | return $tag; |
141 | | - }, $spec['tags']); |
| 157 | + }, $spec->tags); |
142 | 158 | } |
143 | 159 |
|
144 | | -function rewriteOperations(array $spec): array { |
| 160 | +function rewriteOperations(object $spec): array { |
145 | 161 | global $firstStatusCode; |
146 | 162 |
|
147 | | - foreach (array_keys($spec['paths']) as $path) { |
148 | | - foreach (array_keys($spec['paths'][$path]) as $method) { |
| 163 | + foreach (array_keys(get_object_vars($spec->paths)) as $path) { |
| 164 | + foreach (array_keys(get_object_vars($spec->paths->{$path})) as $method) { |
149 | 165 | if (!in_array($method, ['delete', 'get', 'post', 'put', 'patch', 'options'])) { |
150 | 166 | continue; |
151 | 167 | } |
152 | | - $operation = &$spec['paths'][$path][$method]; |
153 | | - if (array_key_exists('operationId', $operation)) { |
154 | | - $operation['operationId'] = $spec['info']['title'] . '-' . $operation['operationId']; |
| 168 | + $operation = &$spec->paths->{$path}->{$method}; |
| 169 | + if (property_exists($operation, 'operationId')) { |
| 170 | + $operation->operationId = $spec->info->title . '-' . $operation->operationId; |
155 | 171 | } |
156 | | - if (array_key_exists('tags', $operation)) { |
157 | | - $operation['tags'] = array_map(fn (string $tag): string => $spec['info']['title'] . '/' . $tag, $operation['tags']); |
| 172 | + if (property_exists($operation, 'tags')) { |
| 173 | + $operation->tags = array_map(static fn (string $tag): string => $spec->info->title . '/' . $tag, $operation->tags); |
158 | 174 | } else { |
159 | | - $operation['tags'] = [$spec['info']['title']]; |
| 175 | + $operation->tags = [$spec->info->title]; |
160 | 176 | } |
161 | | - if ($firstStatusCode && array_key_exists('responses', $operation)) { |
| 177 | + if ($firstStatusCode && property_exists($operation, 'responses')) { |
162 | 178 | /** @var string $value */ |
163 | | - $value = array_key_first($operation['responses']); |
164 | | - $operation['responses'] = [$value => $operation['responses'][$value]]; |
165 | | - } |
166 | | - if (array_key_exists('security', $operation)) { |
167 | | - $counter = count($operation['security']); |
168 | | - for ($i = 0; $i < $counter; $i++) { |
169 | | - if (count($operation['security'][$i]) == 0) { |
170 | | - $operation['security'][$i] = new stdClass(); // When reading {} will be converted to [], so we have to fix it |
171 | | - } |
172 | | - } |
| 179 | + $value = array_key_first(get_object_vars($operation->responses)); |
| 180 | + $response = $operation->responses->{$value}; |
| 181 | + $operation->responses = new stdClass(); |
| 182 | + $operation->responses->{$value} = $response; |
173 | 183 | } |
174 | 184 | } |
175 | 185 | } |
176 | | - return $spec['paths']; |
| 186 | + return get_object_vars($spec->paths); |
177 | 187 | } |
178 | 188 |
|
179 | 189 | file_put_contents($mergedSpecPath, json_encode($data, Helpers::jsonFlags()) . "\n"); |
|
0 commit comments