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