Passed
Pull Request — master (#101)
by Dante
01:16
created

ApiTools::cleanResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * BEdita, API-first content management framework
7
 * Copyright 2025 ChannelWeb Srl, Chialab Srl
8
 *
9
 * This file is part of BEdita: you can redistribute it and/or modify
10
 * it under the terms of the GNU Lesser General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
15
 */
16
namespace BEdita\WebTools\Utility;
17
18
use Cake\Utility\Hash;
19
20
/**
21
 * Api utility methods
22
 */
23
class ApiTools
24
{
25
    /**
26
     * Remove included from response.
27
     *
28
     * @param array $response The response from api client
29
     * @return array
30
     */
31
    public static function removeIncluded(array $response): array
32
    {
33
        return (array)Hash::remove($response, 'included');
34
    }
35
36
    /**
37
     * Remove `links` from response (recursively).
38
     *
39
     * @param array $response The response from api client
40
     * @return array
41
     */
42
    public static function removeLinks(array $response): array
43
    {
44
        return self::recursiveRemoveKey($response, 'links');
45
    }
46
47
    /**
48
     * Remove `relationships` from response (recursively).
49
     *
50
     * @param array $response The response from api client
51
     * @return array
52
     */
53
    public static function removeRelationships(array $response): array
54
    {
55
        return self::recursiveRemoveKey($response, 'relationships');
56
    }
57
58
    /**
59
     * Remove `schema` from response.
60
     *
61
     * @param array $response The response from api client
62
     * @return array
63
     */
64
    public static function removeSchema(array $response): array
65
    {
66
        return (array)Hash::remove($response, 'meta.schema');
67
    }
68
69
    /**
70
     * Remove a key in an array recursively.
71
     *
72
     * @param array $data The starting data
73
     * @param string $key The key to remove
74
     * @return array
75
     */
76
    public static function recursiveRemoveKey(array $data, string $key): array
77
    {
78
        foreach ($data as $k => $v) {
79
            if (is_array($v)) {
80
                $data[$k] = self::recursiveRemoveKey($v, $key);
81
            }
82
        }
83
84
        return array_filter(
85
            $data,
86
            function ($k) use ($key) {
87
                return $k !== $key;
88
            },
89
            ARRAY_FILTER_USE_KEY
90
        );
91
    }
92
93
    /**
94
     * Clean response.
95
     *
96
     * @param array $response The response.
97
     * @param array $options The options to clean.
98
     * @return array
99
     */
100
    public static function cleanResponse(
101
        array $response,
102
        array $options = ['included', 'links', 'schema', 'relationships']
103
    ): array {
104
        foreach ($options as $option) {
105
            $method = 'remove' . ucfirst($option);
106
            $response = self::$method($response);
107
        }
108
109
        return $response;
110
    }
111
}
112