summaryrefslogtreecommitdiff
path: root/build/Strip.php
diff options
context:
space:
mode:
authorZhineng Li <[email protected]>2026-02-14 10:08:30 +0800
committerZhineng Li <[email protected]>2026-02-14 10:08:30 +0800
commiteccd5cd5a067f5659982685afdeea209b1888efb (patch)
tree1626c903b854317be1708ce3ccd3b4f4e6e0fc97 /build/Strip.php
downloadacs-metadata-eccd5cd5a067f5659982685afdeea209b1888efb.tar.gz
acs-metadata-eccd5cd5a067f5659982685afdeea209b1888efb.zip
Diffstat (limited to 'build/Strip.php')
-rw-r--r--build/Strip.php189
1 files changed, 189 insertions, 0 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @link https://api.aliyun.com/openmeta/guide
+ * @link https://api.alibabacloud.com/openmeta/guide
+ */
+final class Strip
+{
+ /**
+ * Strip the list of product info.
+ *
+ * @param mixed[][] $products
+ * @return mixed[][]
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/ProductInfo
+ */
+ public static function product(array $products): array
+ {
+ return array_map(static fn (array $product) => Arr::only($product, [
+ 'code', 'style', 'versions', 'defaultVersion',
+ ]), $products);
+ }
+
+ /**
+ * Strip the API docs.
+ *
+ * @param array<string, mixed> $apiDocs
+ * @return array<string, mixed>
+ *
+ * @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<int, mixed[]> $parameter
+ * @return array<int, mixed[]>
+ *
+ * @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;
+ }
+}