From eccd5cd5a067f5659982685afdeea209b1888efb Mon Sep 17 00:00:00 2001 From: Zhineng Li Date: Sat, 14 Feb 2026 10:08:30 +0800 Subject: first commit --- build/.gitignore | 2 + build/Arr.php | 26 ++++++++ build/Strip.php | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++ build/composer.json | 12 ++++ build/main.php | 31 +++++++++ 5 files changed, 260 insertions(+) create mode 100644 build/.gitignore create mode 100644 build/Arr.php create mode 100644 build/Strip.php create mode 100644 build/composer.json create mode 100644 build/main.php (limited to 'build') diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..d1502b0 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock diff --git a/build/Arr.php b/build/Arr.php new file mode 100644 index 0000000..e9f357d --- /dev/null +++ b/build/Arr.php @@ -0,0 +1,26 @@ + $array + * @param array $keys + * @return array + */ + public static function only(array $array, array $keys): array + { + $result = []; + + foreach ($keys as $key) { + if (array_key_exists($key, $array)) { + $result[$key] = $array[$key]; + } + } + + return $result; + } +} diff --git a/build/Strip.php b/build/Strip.php new file mode 100644 index 0000000..1b0bb0a --- /dev/null +++ b/build/Strip.php @@ -0,0 +1,189 @@ + Arr::only($product, [ + 'code', 'style', 'versions', 'defaultVersion', + ]), $products); + } + + /** + * Strip the API docs. + * + * @param array $apiDocs + * @return array + * + * @link https://api.alibabacloud.com/openmeta/struct/ApiDocs + */ + public static function apiDocs(array $apiDocs): array + { + $data = Arr::only($apiDocs, [ + 'version', 'info', 'components', 'apis', 'endpoints', + ]); + + if (isset($data['apis']) && is_array($data['apis'])) { + $data['apis'] = array_map( + static fn (array $api): array => static::api($api), + $data['apis'] + ); + } + + if (isset($data['components']['schemas']) && is_array($data['components']['schemas'])) { + $data['components']['schemas'] = array_map( + static fn (array $schema): array => static::schema($schema), + $data['components']['schemas'] + ); + } + + return $data; + } + + /** + * Strip the API data structure. + * + * @param mixed[] $api + * @return mixed[] + * + * @link https://api.alibabacloud.com/openmeta/struct/Api + */ + public static function api(array $api): array + { + $data = Arr::only($api, [ + 'path', 'methods', 'schemes', 'security', 'consumes', + 'produces', 'deprecated', 'parameters', + ]); + + if (isset($data['parameters']) && is_array($data['parameters'])) { + $data['parameters'] = array_map( + static fn (array $parameter): array => static::parameter($parameter), + $data['parameters'] + ); + } + + if (isset($api['responses']) && is_array($api['responses']) && static::shouldIncludeResponse($api)) { + $data['responses'] = array_map( + static fn (array $response): array => static::response($response), + $api['responses'] + ); + } + + return $data; + } + + /** + * Strip the parameter data structure. + * + * @param array $parameter + * @return array + * + * @link https://api.alibabacloud.com/openmeta/struct/Parameter + */ + public static function parameter(array $parameter): array + { + $data = Arr::only($parameter, ['name', 'in', 'style', 'schema']); + + if (isset($data['schema']) && is_array($data['schema'])) { + $data['schema'] = static::schema($data['schema']); + } + + return $data; + + } + + /** + * Strip the schema data structure. + * + * @param mixed[] $schema + * @return mixed[] + * + * @link https://api.alibabacloud.com/openmeta/struct/Schema + */ + public static function schema(array $schema): array + { + $data = Arr::only($schema, [ + '$ref', 'type', 'format', 'deprecated', 'required', + 'minimum', 'exclusiveMinimum', 'maximum', 'exclusiveMaximum', + 'minLength', 'maxLength', 'enum', 'pattern', 'properties', + 'additionalProperties', 'items', 'minItems', 'maxItems', + ]); + + if (isset($data['properties']) && is_array($data['properties'])) { + $data['properties'] = array_map(static fn (array $property) => static::schema($property), $data['properties']); + } + + if (isset($data['additionalProperties']) && is_array($data['additionalProperties'])) { + $data['additionalProperties'] = static::schema($data['additionalProperties']); + } + + if (isset($data['items']) && is_array($data['items'])) { + $data['items'] = static::schema($data['items']); + } + + return $data; + } + + /** + * Strip the response data structure. + * + * @param mixed[] $response + * @return mixed[] + * + * @link https://api.alibabacloud.com/openmeta/struct/Response + */ + public static function response(array $response): array + { + $data = Arr::only($response, ['schema']); + + if (isset($data['schema']) && is_array($data['schema'])) { + $data['schema'] = static::schema($data['schema']); + } + + return $data; + } + + /** + * Determine whether to include the response data structure. + * + * @param mixed[] $api + */ + private static function shouldIncludeResponse(array $api): bool + { + // Response schemas are required to correctly parse XML bodies + // and ensure consistent data structures. + return static::consumesXml($api); + } + + /** + * Determine whether the API consumes XML content. + * + * @param mixed[] $api + */ + private static function consumesXml(array $api): bool + { + if (isset($api['consumes']) && is_array($api['consumes'])) { + foreach ($api['consumes'] as $contentType) { + if (is_string($contentType) && str_contains($contentType, 'xml')) { + return true; + } + } + } + + return false; + } +} diff --git a/build/composer.json b/build/composer.json new file mode 100644 index 0000000..07a548e --- /dev/null +++ b/build/composer.json @@ -0,0 +1,12 @@ +{ + "require": { + "php": "^8.4", + "symfony/var-exporter": "^8.0" + }, + "autoload": { + "classmap": [ + "Arr.php", + "Strip.php" + ] + } +} diff --git a/build/main.php b/build/main.php new file mode 100644 index 0000000..0446be3 --- /dev/null +++ b/build/main.php @@ -0,0 +1,31 @@ + Processing %s %s'.PHP_EOL, $product['code'], $version); + + $apiDocsPath = sprintf('%s/%s/%s/api-docs.php', __DIR__.'/../data', $product['code'], $version); + $apiDocs = Strip::apiDocs(require $apiDocsPath); + + $contents = sprintf(' Done!'.PHP_EOL); -- cgit v1.2.3