summaryrefslogtreecommitdiff
path: root/build/Strip.php
blob: 1b0bb0ac86884a1eed2294c26195b9978970729f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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;
    }
}