Passed
Pull Request — master (#1224)
by Dante
02:07
created

ApiTools   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A cleanResponse() 0 23 5
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
16
namespace App\Utility;
17
18
use Cake\Utility\Hash;
19
20
/**
21
 * Api utility methods
22
 */
23
class ApiTools
24
{
25
    /**
26
     * Clean response.
27
     *
28
     * @param array $response The response.
29
     * @param array $remove The keys to remove.
30
     * @return array
31
     */
32
    public static function cleanResponse(
33
        array $response,
34
        array $remove = [
35
            'fromData' => ['links', 'relationships'],
36
            'fromMeta' => ['schema'],
37
        ]
38
    ): array {
39
        $data = (array)Hash::get($response, 'data');
40
        $removeFromData = (array)Hash::get($remove, 'fromData');
41
        foreach ($data as &$item) {
42
            foreach ($removeFromData as $key) {
43
                unset($item[$key]);
44
            }
45
        }
46
        $meta = (array)Hash::get($response, 'meta');
47
        $removeFromMeta = (array)Hash::get($remove, 'fromMeta');
48
        foreach ($meta as $metaKey => &$item) {
49
            if (in_array($metaKey, $removeFromMeta)) {
50
                unset($meta[$metaKey]);
51
            }
52
        }
53
54
        return compact('data', 'meta');
55
    }
56
}
57