Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/Component/ApiFormatterComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function extractTranslatedFields(array $data, string $lang): array
*/
public function cleanResponse(
array $response,
array $options = ['included', 'links', 'schema', 'relationships']
array $options = ['included', 'links', 'schema', 'relationships', 'attributes' => []]
): array {
return ApiTools::cleanResponse($response, $options);
}
Expand Down
50 changes: 48 additions & 2 deletions src/Utility/ApiTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,47 @@
*/
class ApiTools
{
/**
* Remove attributes from response.
*
* @param array $response The response from api client
* @param array $keysToRemove The keys to remove
* @return array
*/
public static function removeAttributes(array $response, array $keysToRemove): array
{
if (empty($keysToRemove) || !isset($response['data'])) {
return $response;
}

// response.data is an array representing a single entity
if (isset($response['data']['attributes'])) {
$response['data']['attributes'] = array_diff_key($response['data']['attributes'], array_flip($keysToRemove));

// remove attributes from included entities
if (isset($response['included'])) {
foreach ($response['included'] as $key => $entity) {
$response['included'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove));
}
}

return $response;
}
// response.data is a list of entities
foreach ($response['data'] as $key => $entity) {
$response['data'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove));
}

// remove attributes from included entities
if (isset($response['included'])) {
foreach ($response['included'] as $key => $entity) {
$response['included'][$key]['attributes'] = array_diff_key($entity['attributes'], array_flip($keysToRemove));
}
}

return $response;
}

/**
* Remove included from response.
*
Expand Down Expand Up @@ -101,9 +142,14 @@ function ($k) use ($key) {
*/
public static function cleanResponse(
array $response,
array $options = ['included', 'links', 'schema', 'relationships']
array $options = ['included', 'links', 'schema', 'relationships', 'attributes' => []]
): array {
foreach ($options as $option) {
foreach ($options as $key => $option) {
if (is_string($key) && is_array($option)) {
$method = 'remove' . ucfirst($key);
$response = self::$method($response, $option);
continue;
}
$method = 'remove' . ucfirst($option);
$response = self::$method($response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ public function testCleanResponse(): void
'title' => 'gustavo supporto',
'name' => 'gustavo',
'surname' => 'supporto',
'extra' => ['some' => 'thing'],
],
'links' => [
'self' => 'https://api.example.org/users/1',
Expand Down Expand Up @@ -809,6 +810,7 @@ public function testCleanResponse(): void
'type' => 'roles',
'attributes' => [
'name' => 'admin',
'extra' => ['any' => 'thing'],
],
],
],
Expand All @@ -828,6 +830,7 @@ public function testCleanResponse(): void
'title' => 'gustavo supporto',
'name' => 'gustavo',
'surname' => 'supporto',
'extra' => ['some' => 'thing'],
],
],
],
Expand Down
Loading
Loading