Mutator::mutatorPutInBetweenKeys()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 4
dl 0
loc 24
rs 9.2222
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Traits;
5
6
/**
7
 * Trait Mutator
8
 *
9
 * @package Keppler\Url\Traits
10
 */
11
trait Mutator
12
{
13
    /**
14
     * @param $array
15
     * @param $value
16
     */
17
    protected function mutatorAppend(array &$array, $value)
18
    {
19
        array_push($array, $value);
20
    }
21
22
    /**
23
     * @param $array
24
     * @param $value
25
     */
26
    protected function mutatorPrepend(array &$array, $value)
27
    {
28
        array_unshift($array, $value);
29
    }
30
31
    /**
32
     * @param array $array
33
     * @param       $before
34
     * @param       $value
35
     *
36
     * @return array
37
     */
38
    protected function mutatorPutBeforeValue(array $array, $before, $value)
39
    {
40
        $result = [];
41
42
        foreach ($array as $key => $item) {
43
            if ($before === $item) {
44
                $result[] = $value;
45
            }
46
47
            $result[] = $item;
48
        }
49
50
        return $result;
51
    }
52
53
    /**
54
     * @param array $array
55
     * @param       $after
56
     * @param       $value
57
     *
58
     * @return array
59
     */
60
    protected function mutatorPutAfterValue(array $array, $after, $value)
61
    {
62
        $result = [];
63
64
        foreach ($array as $key => $item) {
65
            $result[] = $item;
66
67
            if ($after === $item) {
68
                $result[] = $value;
69
            }
70
        }
71
72
        return $result;
73
    }
74
75
    /**
76
     * @param array $array
77
     * @param       $first
78
     * @param       $last
79
     * @param       $value
80
     */
81
    protected function mutatorPutInBetweenKeys(
82
        array &$array,
83
        $value,
84
        $first,
85
        $last
86
    ) {
87
        $position = 0;
88
        if (null !== $first) {
89
            foreach ($array as $key => $item) {
90
                $position++;
91
                if ($first === $item) {
92
                    break;
93
                }
94
            }
95
        } else {
96
            foreach ($array as $key => $item) {
97
                if ($last === $item) {
98
                    break;
99
                }
100
                $position++;
101
            }
102
        }
103
104
        array_splice($array, $position, 0, $value);
105
    }
106
107
    /**
108
     * @param array $array
109
     * @param mixed ...$args
110
     *
111
     * @return array
112
     */
113
    protected function mutatorOnlyPathValues(array $array, ...$args)
114
    {
115
        $result = [];
116
117
        foreach ($array as $item) {
118
            if (!in_array($item, $args[0])) {
119
                continue;
120
            }
121
122
            $result[] = $item;
123
        }
124
125
        return $result;
126
    }
127
128
    /**
129
     * @param array $array
130
     * @param mixed ...$args
131
     * @return array
132
     */
133
    protected function mutatorQueryOnlyValues(array $array, ...$args)
134
    {
135
        $result = [];
136
137
        foreach ($array as $key => $item) {
138
            if (!in_array($key, $args[0])) {
139
                continue;
140
            }
141
142
            $result[$key] = $item;
143
        }
144
145
        return $result;
146
    }
147
148
    /**
149
     * @param $from
150
     * @param $keyOrValue
151
     */
152
    protected function mutatorForgetKeyOrValue(array &$from, $keyOrValue)
153
    {
154
        if (isset($from[$keyOrValue])) {
155
            unset($from[$keyOrValue]);
156
157
            return;
158
        }
159
160
        foreach ($from as $key => $value) {
161
            if ($value === $keyOrValue) {
162
                unset($from[$key]);
163
                break;
164
            }
165
        }
166
    }
167
168
    /**
169
     * @param $what
170
     *
171
     * @return bool
172
     */
173
    protected function isEmpty($what)
174
    {
175
        return empty($what);
176
    }
177
}