|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2025 ChannelWeb Srl, Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace BEdita\WebTools\Utility; |
|
16
|
|
|
|
|
17
|
|
|
use Cake\Utility\Hash; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Api utility methods |
|
21
|
|
|
*/ |
|
22
|
|
|
class ApiTools |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Remove attributes from response. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $response The response from api client |
|
28
|
|
|
* @param array $keysToRemove The keys to remove |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function removeAttributes(array $response, array $keysToRemove): array |
|
32
|
|
|
{ |
|
33
|
|
|
if (empty($keysToRemove) || !isset($response['data'])) { |
|
34
|
|
|
return $response; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// response.data is an array representing a single entity |
|
38
|
|
|
if (isset($response['data']['attributes'])) { |
|
39
|
|
|
$response['data']['attributes'] = array_diff_key( |
|
40
|
|
|
$response['data']['attributes'], |
|
41
|
|
|
array_flip($keysToRemove), |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
// remove attributes from included entities |
|
45
|
|
|
if (isset($response['included'])) { |
|
46
|
|
|
foreach ($response['included'] as $key => $entity) { |
|
47
|
|
|
$response['included'][$key]['attributes'] = array_diff_key( |
|
48
|
|
|
$entity['attributes'], |
|
49
|
|
|
array_flip($keysToRemove), |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
// response.data is a list of entities |
|
57
|
|
|
foreach ($response['data'] as $key => $entity) { |
|
58
|
|
|
$response['data'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// remove attributes from included entities |
|
62
|
|
|
if (isset($response['included'])) { |
|
63
|
|
|
foreach ($response['included'] as $key => $entity) { |
|
64
|
|
|
$response['included'][$key]['attributes'] = array_diff_key( |
|
65
|
|
|
$entity['attributes'], |
|
66
|
|
|
array_flip($keysToRemove), |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $response; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Remove included from response. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $response The response from api client |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function removeIncluded(array $response): array |
|
81
|
|
|
{ |
|
82
|
|
|
return (array)Hash::remove($response, 'included'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Remove `links` from response (recursively). |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $response The response from api client |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function removeLinks(array $response): array |
|
92
|
|
|
{ |
|
93
|
|
|
$response = (array)Hash::remove($response, 'links'); |
|
94
|
|
|
|
|
95
|
|
|
return self::recursiveRemoveKey($response, 'links'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Remove `relationships` from response (recursively). |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $response The response from api client |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function removeRelationships(array $response): array |
|
105
|
|
|
{ |
|
106
|
|
|
return self::recursiveRemoveKey($response, 'relationships'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Remove `schema` from response. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $response The response from api client |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function removeSchema(array $response): array |
|
116
|
|
|
{ |
|
117
|
|
|
return (array)Hash::remove($response, 'meta.schema'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Remove a key in an array recursively. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $data The starting data |
|
124
|
|
|
* @param string $key The key to remove |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function recursiveRemoveKey(array $data, string $key): array |
|
128
|
|
|
{ |
|
129
|
|
|
foreach ($data as $k => $v) { |
|
130
|
|
|
if (is_array($v)) { |
|
131
|
|
|
$data[$k] = self::recursiveRemoveKey($v, $key); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return array_filter( |
|
136
|
|
|
$data, |
|
137
|
|
|
function ($k) use ($key) { |
|
138
|
|
|
return $k !== $key; |
|
139
|
|
|
}, |
|
140
|
|
|
ARRAY_FILTER_USE_KEY, |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Clean response. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $response The response. |
|
148
|
|
|
* @param array $options The options to clean. |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function cleanResponse( |
|
152
|
|
|
array $response, |
|
153
|
|
|
array $options = ['included', 'links', 'schema', 'relationships', 'attributes' => []], |
|
154
|
|
|
): array { |
|
155
|
|
|
foreach ($options as $key => $option) { |
|
156
|
|
|
if (is_string($key) && is_array($option)) { |
|
157
|
|
|
$method = 'remove' . ucfirst($key); |
|
158
|
|
|
$response = self::$method($response, $option); |
|
159
|
|
|
continue; |
|
160
|
|
|
} |
|
161
|
|
|
$method = 'remove' . ucfirst($option); |
|
162
|
|
|
$response = self::$method($response); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $response; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|