Completed
Push — master ( b139e2...07b7da )
by Daniel
03:29
created

src/Transform.php (1 issue)

parameters are used.

Unused Code Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Narrowspark\Arr;
3
4
use Narrowspark\Arr\Traits\SplitPathTrait;
5
use Narrowspark\Arr\Traits\ValueTrait;
6
7
class Transform
8
{
9
    /**
10
     * Dotted array cache.
11
     *
12
     * @var array
13
     */
14
    protected $dotted = [];
15
16
    /**
17
     * Swap two elements between positions.
18
     *
19
     * @param array  $array array to swap
20
     * @param string $swapA
21
     * @param string $swapB
22
     *
23
     * @return array|null
24
     */
25
    public function swap(array $array, $swapA, $swapB)
26
    {
27
        list($array[$swapA], $array[$swapB]) = [$array[$swapB], $array[$swapA]];
28
    }
29
30
    /**
31
     * Indexes an array depending on the values it contains.
32
     *
33
     * @param array    $array
34
     * @param callable $callback  Function to combine values.
35
     * @param bool     $overwrite Should duplicate keys be overwritten?
36
     *
37
     * @return array Indexed values.
38
     */
39
    public function combine(array $array, callable $callback, $overwrite = true)
40
    {
41
        $combined = [];
42
43
        foreach ($array as $key => $value) {
44
            $combinator = call_user_func($callback, $value, $key);
45
            $index      = $combinator->key();
46
47
            if ($overwrite || !isset($combined[$index])) {
48
                $combined[$index] = $combinator->current();
49
            }
50
        }
51
52
        return $combined;
53
    }
54
55
    /**
56
     * Collapse an array of arrays into a single array.
57
     *
58
     * @param array $array
59
     *
60
     * @return array
61
     */
62
    public function collapse(array $array)
63
    {
64
        $results = [];
65
66
        foreach ($array as $values) {
67
            if (!is_array($values)) {
68
                continue;
69
            }
70
71
            $results = array_merge($results, $values);
72
        }
73
74
        return $results;
75
    }
76
77
    /**
78
     * Divide an array into two arrays. One with keys and the other with values.
79
     *
80
     * @param array $array
81
     *
82
     * @return array[]
83
     */
84
    public function divide($array)
85
    {
86
        return [array_keys($array), array_values($array)];
87
    }
88
89
    /**
90
     * Reindexes a list of values.
91
     *
92
     * @param array $array
93
     * @param array $map          An map of correspondances of the form
94
     *                            ['currentIndex' => 'newIndex'].
95
     * @param bool  $keepUnmapped Whether or not to keep keys that are not
96
     *                            remapped.
97
     *
98
     * @return array
99
     */
100
    public function reindex(array $array, array $map, $keepUnmapped = true)
101
    {
102
        $reindexed = $keepUnmapped
103
            ? $array
104
            : [];
105
106
        foreach ($map as $from => $to) {
107
            if (isset($array[$from])) {
108
                $reindexed[$to] = $array[$from];
109
            }
110
        }
111
112
        return $reindexed;
113
    }
114
115
    /**
116
     * Merges two arrays recursively.
117
     *
118
     * @param array $first  Original data.
119
     * @param array $second Data to be merged.
120
     *
121
     * @return array
122
     */
123
    public function merge(array $first, array $second)
124
    {
125
        foreach ($second as $key => $value) {
126
            $shouldBeMerged = (
127
                isset($first[$key])
128
                && is_array($first[$key])
129
                && is_array($value)
130
            );
131
132
            $first[$key] = $shouldBeMerged
133
                ? $this->merge($first[$key], $value)
134
                : $value;
135
        }
136
137
        return $first;
138
    }
139
140
    /**
141
     * Extend one array with another.
142
     *
143
     * @param array $arrays
144
     *
145
     * @return array
146
     */
147
    public function extend(array $arrays)
1 ignored issue
show
The parameter $arrays is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        $merged = [];
150
151
        foreach (func_get_args() as $array) {
152
            foreach ($array as $key => $value) {
153
                if (is_array($value) && (new Access())->has($merged, $key) && is_array($merged[$key])) {
154
                    $merged[$key] = $this->extend($merged[$key], $value);
155
                } else {
156
                    $merged[$key] = $value;
157
                }
158
            }
159
        }
160
161
        return $merged;
162
    }
163
164
    /**
165
     * Flatten a multi-dimensional associative array with dots.
166
     *
167
     * @param array  $array
168
     * @param string $prepend
169
     *
170
     * @return array
171
     */
172
    public function dot($array, $prepend = '')
173
    {
174
        $cache = serialize(['array' => $array, 'prepend' => $prepend]);
175
176
        if (array_key_exists($cache, $this->dotted)) {
177
            return $this->dotted[$cache];
178
        }
179
180
        $results = [];
181
182 View Code Duplication
        foreach ($array as $key => $value) {
183
            if (is_array($value)) {
184
                $results = array_merge($results, $this->dot($value, $prepend . $key . '.'));
185
            } else {
186
                $results[$prepend . $key] = $value;
187
            }
188
        }
189
190
        return $this->dotted[$cache] = $results;
191
    }
192
193
    /**
194
     * Flatten a nested array to a separated key.
195
     *
196
     * @param array       $array
197
     * @param string|null $separator
198
     * @param string      $prepend
199
     *
200
     * @return array
201
     */
202
    public function flatten(array $array, $separator = null, $prepend = '')
203
    {
204
        $flattened = [];
205
206 View Code Duplication
        foreach ($array as $key => $value) {
207
            if (is_array($value)) {
208
                $flattened = array_merge($flattened, $this->flatten($value, $separator, $prepend . $key . $separator));
209
            } else {
210
                $flattened[$prepend . $key] = $value;
211
            }
212
        }
213
214
        return $flattened;
215
    }
216
}
217