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 attributes from response. |
27
|
|
|
* |
28
|
|
|
* @param array $response The response from api client |
29
|
|
|
* @param array $keysToRemove The keys to remove |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public static function removeAttributes(array $response, array $keysToRemove): array |
33
|
|
|
{ |
34
|
|
|
if (empty($keysToRemove) || !isset($response['data'])) { |
35
|
|
|
return $response; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// response.data is an array representing a single entity |
39
|
|
|
if (isset($response['data']['attributes'])) { |
40
|
|
|
$response['data']['attributes'] = array_diff_key($response['data']['attributes'], array_flip($keysToRemove)); |
41
|
|
|
|
42
|
|
|
// remove attributes from included entities |
43
|
|
|
if (isset($response['included'])) { |
44
|
|
|
foreach ($response['included'] as $key => $entity) { |
45
|
|
|
$response['included'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove)); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $response; |
50
|
|
|
} |
51
|
|
|
// response.data is a list of entities |
52
|
|
|
foreach ($response['data'] as $key => $entity) { |
53
|
|
|
$response['data'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// remove attributes from included entities |
57
|
|
|
if (isset($response['included'])) { |
58
|
|
|
foreach ($response['included'] as $key => $entity) { |
59
|
|
|
$response['included'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Remove included from response. |
68
|
|
|
* |
69
|
|
|
* @param array $response The response from api client |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public static function removeIncluded(array $response): array |
73
|
|
|
{ |
74
|
|
|
return (array)Hash::remove($response, 'included'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remove `links` from response (recursively). |
79
|
|
|
* |
80
|
|
|
* @param array $response The response from api client |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public static function removeLinks(array $response): array |
84
|
|
|
{ |
85
|
|
|
$response = (array)Hash::remove($response, 'links'); |
86
|
|
|
|
87
|
|
|
return self::recursiveRemoveKey($response, 'links'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Remove `relationships` from response (recursively). |
92
|
|
|
* |
93
|
|
|
* @param array $response The response from api client |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public static function removeRelationships(array $response): array |
97
|
|
|
{ |
98
|
|
|
return self::recursiveRemoveKey($response, 'relationships'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Remove `schema` from response. |
103
|
|
|
* |
104
|
|
|
* @param array $response The response from api client |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public static function removeSchema(array $response): array |
108
|
|
|
{ |
109
|
|
|
return (array)Hash::remove($response, 'meta.schema'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Remove a key in an array recursively. |
114
|
|
|
* |
115
|
|
|
* @param array $data The starting data |
116
|
|
|
* @param string $key The key to remove |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public static function recursiveRemoveKey(array $data, string $key): array |
120
|
|
|
{ |
121
|
|
|
foreach ($data as $k => $v) { |
122
|
|
|
if (is_array($v)) { |
123
|
|
|
$data[$k] = self::recursiveRemoveKey($v, $key); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return array_filter( |
128
|
|
|
$data, |
129
|
|
|
function ($k) use ($key) { |
130
|
|
|
return $k !== $key; |
131
|
|
|
}, |
132
|
|
|
ARRAY_FILTER_USE_KEY |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Clean response. |
138
|
|
|
* |
139
|
|
|
* @param array $response The response. |
140
|
|
|
* @param array $options The options to clean. |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public static function cleanResponse( |
144
|
|
|
array $response, |
145
|
|
|
array $options = ['included', 'links', 'schema', 'relationships', 'attributes' => []] |
146
|
|
|
): array { |
147
|
|
|
foreach ($options as $key => $option) { |
148
|
|
|
if (is_string($key) && is_array($option)) { |
149
|
|
|
$method = 'remove' . ucfirst($key); |
150
|
|
|
$response = self::$method($response, $option); |
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
$method = 'remove' . ucfirst($option); |
154
|
|
|
$response = self::$method($response); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $response; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|