RecursiveDeleteHelper   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 17
c 2
b 0
f 1
lcom 0
cbo 1
dl 0
loc 93
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteKeys() 0 10 3
A deleteProperties() 0 12 3
A unsetKeys() 0 8 3
A deleteMatchedClassProperties() 0 10 4
A deleteNextLevelProperties() 0 16 4
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/24/15
6
 * Time: 8:55 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Api\Transformer\Helpers;
12
13
use NilPortugues\Serializer\Serializer;
14
15
/**
16
 * Class RecursiveDeleteHelper.
17
 */
18
final class RecursiveDeleteHelper
19
{
20
    /**
21
     * Removes array keys matching the $unwantedKey array by using recursion.
22
     *
23
     * @param array $array
24
     * @param array $unwantedKey
25
     */
26
    public static function deleteKeys(array &$array, array $unwantedKey)
27
    {
28
        self::unsetKeys($array, $unwantedKey);
29
30
        foreach ($array as &$value) {
31
            if (\is_array($value)) {
32
                self::deleteKeys($value, $unwantedKey);
33
            }
34
        }
35
    }
36
37
    /**
38
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
39
     * @param array                               $array
40
     * @param string                              $typeKey
41
     * @param array                               $deletions
42
     * @param array                               $newArray
43
     */
44
    private static function deleteNextLevelProperties(
45
        array &$mappings,
46
        array &$array,
47
        $typeKey,
48
        array &$deletions,
49
        array &$newArray
50
    ) {
51
        foreach ($array as $key => &$value) {
52
            if (!in_array($key, $deletions, true)) {
53
                $newArray[$key] = $value;
54
                if (\is_array($newArray[$key])) {
55
                    self::deleteProperties($mappings, $newArray[$key], $typeKey);
56
                }
57
            }
58
        }
59
    }
60
61
    /**
62
     * Removes a sets if keys for a given class using recursion.
63
     *
64
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
65
     * @param array                               $array    Array with data
66
     * @param string                              $typeKey  Scope to do the replacement.
67
     */
68
    public static function deleteProperties(array &$mappings, array &$array, $typeKey)
69
    {
70
        if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $array)) {
71
            $newArray = [];
72
73
            self::deleteMatchedClassProperties($mappings, $array, $typeKey, $newArray);
74
75
            if (!empty($newArray)) {
76
                $array = $newArray;
77
            }
78
        }
79
    }
80
81
    /**
82
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
83
     * @param array                               $array
84
     * @param string                              $typeKey
85
     * @param array                               $newArray
86
     */
87
    private static function deleteMatchedClassProperties(array &$mappings, array &$array, $typeKey, array &$newArray)
88
    {
89
        $type = $array[Serializer::CLASS_IDENTIFIER_KEY];
90
        if (\is_scalar($type) && $type === $typeKey) {
91
            $deletions = $mappings[$typeKey]->getHiddenProperties();
92
            if (!empty($deletions)) {
93
                self::deleteNextLevelProperties($mappings, $array, $typeKey, $deletions, $newArray);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param array $array
100
     * @param array $unwantedKey
101
     */
102
    private static function unsetKeys(array &$array, array &$unwantedKey)
103
    {
104
        foreach ($unwantedKey as $key) {
105
            if (\array_key_exists($key, $array)) {
106
                unset($array[$key]);
107
            }
108
        }
109
    }
110
}
111