Completed
Pull Request — dev (#317)
by Tristan
06:21
created

Controller::rotateKeys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Controller
Loading history...
11
{
12
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
13
14
    public function shiftFirstLevelArrayKeysToBottom(array $nestedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function shiftFirstLevelArrayKeysToBottom()
Loading history...
15
        $expandedArray = $this->expandNestedArraysIntoKeyListAndValue($nestedArray);
16
        $rotatedArray = $this->rotateKeys($expandedArray, 1);
17
        $mergedArray = $this->mergeExpandedTrees($rotatedArray);
18
        return $mergedArray;
19
    }
20
21
    protected function addKeyAsFinalIndex($finalKey, $array) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addKeyAsFinalIndex()
Loading history...
22
        if (!is_array($array)) {
23
            return [$finalKey => $array];
24
        } else {
25
            $newArray = [];
26
            foreach($array as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
27
                $newArray[$key] = $this->addKeyAsFinalIndex($finalKey, $value);
28
            }
29
            return $newArray;
30
        }
31
    }
32
33
    protected function expandNestedArraysIntoKeyListAndValue($nestedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function expandNestedArraysIntoKeyListAndValue()
Loading history...
34
        if (!is_array($nestedArray)) {
35
            $expandedArray = [
36
                [
37
                    'keys' => [],
38
                    'value' => $nestedArray,
39
                ]
40
            ];
41
            return $expandedArray;
42
        } else {
43
            $expandedArray = [];
44
            foreach($nestedArray as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
45
                $subArray = $this->expandNestedArraysIntoKeyListAndValue($value);
46
                foreach($subArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
47
                    array_unshift($item['keys'], $key);
48
                    $expandedArray[] = $item;
49
                }
50
            }
51
            return $expandedArray;
52
        }
53
    }
54
55
    protected function mergeExpandedTrees($expandedArray) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function mergeExpandedTrees()
Loading history...
56
        $mergedArray = [];
57
        foreach($expandedArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
58
            $tail = &$mergedArray;
59
            $size = count($item['keys']);
60
            $i = 0;
61
            foreach($item['keys'] as $key) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
62
                $i = ($i + 1);
63
                //Check if this is the last key
64
                if ($i == ($size)) {
65
                    if (!isset($tail[$key])) {
66
                        $tail[$key] = $item['value'];
67
                    } else if (!is_array($tail[$key])) {
68
                        $value = $tail[$key];
69
                        $tail[$key] = [$value, $item['value']];
70
                    } else {
71
                        array_push($tail[$key], $item['value']);
72
                    }
73
                } else {
74
                    //If this is not the last key, it needs to contain an array
75
                    if (!isset($tail[$key])) {
76
                        $tail[$key] = [];
77
                    } else if (!is_array($tail[$key])) {
78
                        $value = $tail[$key];
79
                        $tail[$key] = [$value];
80
                    }
81
                    $tail = &$tail[$key];
82
                }
83
            }
84
        }
85
        return $mergedArray;
86
    }
87
88
    protected function rotateKeys($expandedArray, $steps) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function rotateKeys()
Loading history...
89
        $rotatedArray = [];
90
        foreach($expandedArray as $item) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
91
            for ($i=0; $i<$steps; $i++) {
92
                array_push($item['keys'], array_shift($item['keys']));
93
            }
94
            $rotatedArray[] = $item;
95
        }
96
        return $rotatedArray;
97
    }
98
}
99