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
|
|
|
$response = (array)Hash::remove($response, 'links'); |
45
|
|
|
|
46
|
|
|
return self::recursiveRemoveKey($response, 'links'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Remove `relationships` from response (recursively). |
51
|
|
|
* |
52
|
|
|
* @param array $response The response from api client |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public static function removeRelationships(array $response): array |
56
|
|
|
{ |
57
|
|
|
return self::recursiveRemoveKey($response, 'relationships'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove `schema` from response. |
62
|
|
|
* |
63
|
|
|
* @param array $response The response from api client |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public static function removeSchema(array $response): array |
67
|
|
|
{ |
68
|
|
|
return (array)Hash::remove($response, 'meta.schema'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Remove a key in an array recursively. |
73
|
|
|
* |
74
|
|
|
* @param array $data The starting data |
75
|
|
|
* @param string $key The key to remove |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public static function recursiveRemoveKey(array $data, string $key): array |
79
|
|
|
{ |
80
|
|
|
foreach ($data as $k => $v) { |
81
|
|
|
if (is_array($v)) { |
82
|
|
|
$data[$k] = self::recursiveRemoveKey($v, $key); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return array_filter( |
87
|
|
|
$data, |
88
|
|
|
function ($k) use ($key) { |
89
|
|
|
return $k !== $key; |
90
|
|
|
}, |
91
|
|
|
ARRAY_FILTER_USE_KEY |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Clean response. |
97
|
|
|
* |
98
|
|
|
* @param array $response The response. |
99
|
|
|
* @param array $options The options to clean. |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public static function cleanResponse( |
103
|
|
|
array $response, |
104
|
|
|
array $options = ['included', 'links', 'schema', 'relationships'] |
105
|
|
|
): array { |
106
|
|
|
foreach ($options as $option) { |
107
|
|
|
$method = 'remove' . ucfirst($option); |
108
|
|
|
$response = self::$method($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $response; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|