1 | <?php |
||
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) |
||
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) |
||
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) |
||
110 | } |
||
111 |