expandNestedArraysIntoKeyListAndValue()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
ccs 0
cts 12
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Foundation\Bus\DispatchesJobs;
6
use Illuminate\Routing\Controller as BaseController;
7
use Illuminate\Foundation\Validation\ValidatesRequests;
8
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
9
10
class Controller extends BaseController
11
{
12
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
13
14 3
    public function getRelativeIds($input, $relativeType)
15
    {
16 3
        $relativeIds = [];
17 3
        if (isset($input['relatives'])) {
18
            $relatives = $input['relatives'];
19
            if (isset($relatives[$relativeType])) {
20
                foreach ($relatives[$relativeType] as $relativeInput) {
21
                    $relativeIds[] = $relativeInput['id'];
22
                }
23
            }
24
        }
25 3
        return $relativeIds;
26
    }
27
28
    public function shiftFirstLevelArrayKeysToBottom(array $nestedArray)
29
    {
30
        $expandedArray = $this->expandNestedArraysIntoKeyListAndValue($nestedArray);
31
        $rotatedArray = $this->rotateKeys($expandedArray, 1);
32
        $mergedArray = $this->mergeExpandedTrees($rotatedArray);
33
        return $mergedArray;
34
    }
35
36
    protected function addKeyAsFinalIndex($finalKey, $array)
37
    {
38
        if (!is_array($array)) {
39
            return [$finalKey => $array];
40
        } else {
41
            $newArray = [];
42
            foreach ($array as $key => $value) {
43
                $newArray[$key] = $this->addKeyAsFinalIndex($finalKey, $value);
44
            }
45
            return $newArray;
46
        }
47
    }
48
49
    protected function expandNestedArraysIntoKeyListAndValue($nestedArray)
50
    {
51
        if (!is_array($nestedArray)) {
52
            $expandedArray = [
53
                [
54
                    'keys' => [],
55
                    'value' => $nestedArray,
56
                ]
57
            ];
58
            return $expandedArray;
59
        } else {
60
            $expandedArray = [];
61
            foreach ($nestedArray as $key => $value) {
62
                $subArray = $this->expandNestedArraysIntoKeyListAndValue($value);
63
                foreach ($subArray as $item) {
64
                    array_unshift($item['keys'], $key);
65
                    $expandedArray[] = $item;
66
                }
67
            }
68
            return $expandedArray;
69
        }
70
    }
71
72
    protected function mergeExpandedTrees($expandedArray)
73
    {
74
        $mergedArray = [];
75
        foreach ($expandedArray as $item) {
76
            $tail = &$mergedArray;
77
            $size = count($item['keys']);
78
            $i = 0;
79
            foreach ($item['keys'] as $key) {
80
                $i = ($i + 1);
81
                // Check if this is the last key.
82
                if ($i == ($size)) {
83
                    if (!isset($tail[$key])) {
84
                        $tail[$key] = $item['value'];
85
                    } elseif (!is_array($tail[$key])) {
86
                        $value = $tail[$key];
87
                        $tail[$key] = [$value, $item['value']];
88
                    } else {
89
                        array_push($tail[$key], $item['value']);
90
                    }
91
                } else {
92
                    // If this is not the last key, it needs to contain an array.
93
                    if (!isset($tail[$key])) {
94
                        $tail[$key] = [];
95
                    } elseif (!is_array($tail[$key])) {
96
                        $value = $tail[$key];
97
                        $tail[$key] = [$value];
98
                    }
99
                    $tail = &$tail[$key];
100
                }
101
            }
102
        }
103
        return $mergedArray;
104
    }
105
106
    protected function rotateKeys($expandedArray, $steps)
107
    {
108
        $rotatedArray = [];
109
        foreach ($expandedArray as $item) {
110
            for ($i = 0; $i < $steps; $i++) {
111
                array_push($item['keys'], array_shift($item['keys']));
112
            }
113
            $rotatedArray[] = $item;
114
        }
115
        return $rotatedArray;
116
    }
117
118
    /**
119
     * Formats the $content array into JSON.
120
     *
121
     * @param string[] $content The array being returned in response.
122
     * @return \Illuminate\Http\Response
123
     */
124
    protected function formatAjaxResponse(array $content)
125
    {
126
        return response()->json($content);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        return /** @scrutinizer ignore-call */ response()->json($content);
Loading history...
127
    }
128
}
129