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
15 changes: 15 additions & 0 deletions src/Controller/Component/ApiFormatterComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace BEdita\WebTools\Controller\Component;

use BEdita\WebTools\Utility\ApiTools;
use Cake\Collection\Collection;
use Cake\Controller\Component;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -158,4 +159,18 @@ protected function extractTranslatedFields(array $data, string $lang): array

return array_filter((array)array_shift($translatedFields));
}

/**
* Clean response from unwanted keys (recursively).
*
* @param array $response The response from API
* @param array $options The options to clean response
* @return array
*/
public function cleanResponse(
array $response,
array $options = ['included', 'links', 'schema', 'relationships']
): array {
return ApiTools::cleanResponse($response, $options);
}
}
113 changes: 113 additions & 0 deletions src/Utility/ApiTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2025 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\WebTools\Utility;

use Cake\Utility\Hash;

/**
* Api utility methods
*/
class ApiTools
{
/**
* Remove included from response.
*
* @param array $response The response from api client
* @return array
*/
public static function removeIncluded(array $response): array
{
return (array)Hash::remove($response, 'included');
}

/**
* Remove `links` from response (recursively).
*
* @param array $response The response from api client
* @return array
*/
public static function removeLinks(array $response): array
{
$response = (array)Hash::remove($response, 'links');

return self::recursiveRemoveKey($response, 'links');
}

/**
* Remove `relationships` from response (recursively).
*
* @param array $response The response from api client
* @return array
*/
public static function removeRelationships(array $response): array
{
return self::recursiveRemoveKey($response, 'relationships');
}

/**
* Remove `schema` from response.
*
* @param array $response The response from api client
* @return array
*/
public static function removeSchema(array $response): array
{
return (array)Hash::remove($response, 'meta.schema');
}

/**
* Remove a key in an array recursively.
*
* @param array $data The starting data
* @param string $key The key to remove
* @return array
*/
public static function recursiveRemoveKey(array $data, string $key): array
{
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = self::recursiveRemoveKey($v, $key);
}
}

return array_filter(
$data,
function ($k) use ($key) {
return $k !== $key;
},
ARRAY_FILTER_USE_KEY
);
}

/**
* Clean response.
*
* @param array $response The response.
* @param array $options The options to clean.
* @return array
*/
public static function cleanResponse(
array $response,
array $options = ['included', 'links', 'schema', 'relationships']
): array {
foreach ($options as $option) {
$method = 'remove' . ucfirst($option);
$response = self::$method($response);
}

return $response;
}
}
102 changes: 102 additions & 0 deletions tests/TestCase/Controller/Component/ApiFormatterComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,106 @@ public function testReplaceWithTranslation(array $expected, array $response, str
$actual = $this->ApiFormatter->replaceWithTranslation($response, $lang);
static::assertEquals($expected, $actual);
}

/**
* Test `cleanResponse()` method.
*
* @return void
* @covers ::cleanResponse()
*/
public function testCleanResponse(): void
{
$response = [
'data' => [
[
'id' => 1,
'attributes' => [
'uname' => 'gustavo',
'title' => 'gustavo supporto',
'name' => 'gustavo',
'surname' => 'supporto',
],
'links' => [
'self' => 'https://api.example.org/users/1',
],
'relationships' => [
'roles' => [
'links' => [
'self' => 'https://api.example.org/users/1/relationships/roles',
'related' => 'https://api.example.org/users/1/roles',
],
],
],
],
],
'meta' => [
'pagination' => [
'page' => 1,
'page_count' => 1,
'page_items' => 1,
'page_size' => 20,
'count' => 1,
],
'schema' => [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
],
'type' => [
'type' => 'string',
],
'attributes' => [
'type' => 'object',
],
'links' => [
'type' => 'object',
],
'relationships' => [
'type' => 'object',
],
],
'required' => ['id', 'type', 'attributes'],
],
],
'included' => [
[
'id' => 1,
'type' => 'roles',
'attributes' => [
'name' => 'admin',
],
],
],
'links' => [
'self' => 'https://api.example.org/users',
'first' => 'https://api.example.org/users?page=1',
'last' => 'https://api.example.org/users?page=1',
],
];
$actual = $this->ApiFormatter->cleanResponse($response);
$expected = [
'data' => [
[
'id' => 1,
'attributes' => [
'uname' => 'gustavo',
'title' => 'gustavo supporto',
'name' => 'gustavo',
'surname' => 'supporto',
],
],
],
'meta' => [
'pagination' => [
'page' => 1,
'page_count' => 1,
'page_items' => 1,
'page_size' => 20,
'count' => 1,
],
],
];
static::assertEquals($expected, $actual);
}
}
133 changes: 133 additions & 0 deletions tests/TestCase/Utility/ApiToolsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2020 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\WebTools\Test\TestCase\Utility;

use BEdita\WebTools\Utility\ApiTools;
use Cake\TestSuite\TestCase;

/**
* {@see \BEdita\WebTools\Utility\ApiTools} Test Case
*
* @coversDefaultClass \BEdita\WebTools\Utility\ApiTools
*/
class ApiToolsTest extends TestCase
{
/**
* Test clean response
*
* @return void
* @covers ::cleanResponse()
* @covers ::recursiveRemoveKey()
* @covers ::removeIncluded()
* @covers ::removeLinks()
* @covers ::removeRelationships()
* @covers ::removeSchema()
*/
public function testCleanResponse(): void
{
$response = [
'data' => [
[
'id' => 1,
'attributes' => [
'uname' => 'gustavo',
'title' => 'gustavo supporto',
'name' => 'gustavo',
'surname' => 'supporto',
],
'links' => [
'self' => 'https://api.example.org/users/1',
],
'relationships' => [
'roles' => [
'links' => [
'self' => 'https://api.example.org/users/1/relationships/roles',
'related' => 'https://api.example.org/users/1/roles',
],
],
],
],
],
'meta' => [
'pagination' => [
'page' => 1,
'page_count' => 1,
'page_items' => 1,
'page_size' => 20,
'count' => 1,
],
'schema' => [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
],
'type' => [
'type' => 'string',
],
'attributes' => [
'type' => 'object',
],
'links' => [
'type' => 'object',
],
'relationships' => [
'type' => 'object',
],
],
'required' => ['id', 'type', 'attributes'],
],
],
'included' => [
[
'id' => 1,
'type' => 'roles',
'attributes' => [
'name' => 'admin',
],
],
],
'links' => [
'self' => 'https://api.example.org/users',
'first' => 'https://api.example.org/users?page=1',
'last' => 'https://api.example.org/users?page=1',
],
];
$actual = ApiTools::cleanResponse($response);
$expected = [
'data' => [
[
'id' => 1,
'attributes' => [
'uname' => 'gustavo',
'title' => 'gustavo supporto',
'name' => 'gustavo',
'surname' => 'supporto',
],
],
],
'meta' => [
'pagination' => [
'page' => 1,
'page_count' => 1,
'page_items' => 1,
'page_size' => 20,
'count' => 1,
],
],
];
static::assertEquals($expected, $actual);
}
}
Loading