Completed
Push — master ( 44cd2c...638a5b )
by Daniel
05:15
created

Transform::collapse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
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 $cb        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 $cb, $overwrite = true)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $cb. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
40
    {
41
        $combined = [];
42
43
        foreach ($array as $key => $value) {
44
            $combinator = call_user_func($cb, $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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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 boole $keepUnmapped Whether or not to keep keys that are not
0 ignored issues
show
Documentation introduced by
Should the type for parameter $keepUnmapped not be boolean|boole?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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
     * Flatten a multi-dimensional associative array with dots.
142
     *
143
     * @param array  $array
144
     * @param string $prepend
145
     *
146
     * @return array
147
     */
148
    public function dot($array, $prepend = '')
149
    {
150
        $cache = serialize(['array' => $array, 'prepend' => $prepend]);
151
152
        if (array_key_exists($cache, $this->dotted)) {
153
            return $this->dotted[$cache];
154
        }
155
156
        $results = [];
157
158 View Code Duplication
        foreach ($array as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
159
            if (is_array($value)) {
160
                $results = array_merge($results, $this->dot($value, $prepend . $key . '.'));
161
            } else {
162
                $results[$prepend . $key] = $value;
163
            }
164
        }
165
166
        return $this->dotted[$cache] = $results;
167
    }
168
169
    /**
170
     * Flatten a nested array to a separated key.
171
     *
172
     * @param array       $array
173
     * @param string|null $separator
174
     * @param string      $prepend
175
     *
176
     * @return array
177
     */
178
    public function flatten(array $array, $separator = null, $prepend = '')
179
    {
180
        $flattened = [];
181
182 View Code Duplication
        foreach ($array as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
183
            if (is_array($value)) {
184
                $flattened = array_merge($flattened, $this->flatten($value, $separator, $prepend . $key . $separator));
185
            } else {
186
                $flattened[$prepend . $key] = $value;
187
            }
188
        }
189
190
        return $flattened;
191
    }
192
}
193