RecursiveRenamerHelper::renameKeys()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 11
nc 5
nop 5
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/24/15
6
 * Time: 8:59 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 RecursiveRenamerHelper.
17
 */
18
class RecursiveRenamerHelper
19
{
20
    /**
21
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
22
     * @param array                               $array
23
     * @param string                              $typeKey
24
     * @param array                               $replacements
25
     * @param array                               $newArray
26
     */
27
    private static function renameKeys(
28
        array &$mappings,
29
        array &$array,
30
        $typeKey,
31
        array &$replacements,
32
        array &$newArray
33
    ) {
34
        foreach ($array as $key => &$value) {
35
            $key = (!empty($replacements[$key])) ? $replacements[$key] : $key;
36
            $newArray[$key] = $value;
37
38
            if (\is_array($newArray[$key])) {
39
                self::renameKeyValue($mappings, $newArray[$key], $typeKey);
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
46
     * @param array                               $array
47
     * @param string                              $typeKey
48
     */
49 View Code Duplication
    public static function renameKeyValue(array &$mappings, array &$array, $typeKey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $array)) {
52
            $newArray = [];
53
            $type = $array[Serializer::CLASS_IDENTIFIER_KEY];
54
55
            self::renameMatchedClassKeys($mappings, $array, $typeKey, $type, $newArray);
56
57
            if (!empty($newArray)) {
58
                $array = $newArray;
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
65
     * @param array                               $array
66
     * @param string                              $typeKey
67
     * @param string                              $type
68
     * @param array                               $newArray
69
     */
70
    private static function renameMatchedClassKeys(array &$mappings, array &$array, $typeKey, $type, array &$newArray)
71
    {
72
        if (\is_scalar($type) && $type === $typeKey) {
73
            $replacements = $mappings[$typeKey]->getAliasedProperties();
74
            if (!empty($replacements)) {
75
                self::renameKeys($mappings, $array, $typeKey, $replacements, $newArray);
76
            }
77
        }
78
    }
79
}
80