Completed
Push — master ( 603fa9...c5e232 )
by Daniel
03:00
created

Transform::divide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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 boolean  $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
     * Divide an array into two arrays. One with keys and the other with values.
57
     *
58
     * @param array $array
59
     *
60
     * @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...
61
     */
62
    public function divide($array)
63
    {
64
        return [array_keys($array), array_values($array)];
65
    }
66
67
    /**
68
     *  Reindexes a list of values.
69
     *
70
     *  @param array $array
71
     *  @param array $map          An map of correspondances of the form
72
     *                             ['currentIndex' => 'newIndex'].
73
     *  @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...
74
     *                             remapped.
75
     *
76
     *  @return array
77
     */
78
    public function reindex(array $array, array $map, $keepUnmapped = true)
79
    {
80
        $reindexed = $keepUnmapped
81
            ? $array
82
            : [];
83
84
        foreach ($map as $from => $to) {
85
            if (isset($array[$from])) {
86
                $reindexed[$to] = $array[$from];
87
            }
88
        }
89
90
        return $reindexed;
91
    }
92
93
    /**
94
     * Merges two arrays recursively.
95
     *
96
     * @param array $first  Original data.
97
     * @param array $second Data to be merged.
98
     *
99
     * @return array
100
     */
101
    public function merge(array $first, array $second)
102
    {
103
        foreach ($second as $key => $value) {
104
            $shouldBeMerged = (
105
                isset($first[$key])
106
                && is_array($first[$key])
107
                && is_array($value)
108
            );
109
110
            $first[$key] = $shouldBeMerged
111
                ? $this->merge($first[$key], $value)
112
                : $value;
113
        }
114
115
        return $first;
116
    }
117
118
    /**
119
     * Flatten a multi-dimensional associative array with dots.
120
     *
121
     * @param array  $array
122
     * @param string $prepend
123
     *
124
     * @return array
125
     */
126
    public function dot($array, $prepend = '')
127
    {
128
        $cache = serialize(['array' => $array, 'prepend' => $prepend]);
129
130
        if (array_key_exists($cache, $this->dotted)) {
131
            return $this->dotted[$cache];
132
        }
133
134
        $results = [];
135
136 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...
137
            if (is_array($value)) {
138
                $results = array_merge($results, $this->dot($value, $prepend . $key . '.'));
139
            } else {
140
                $results[$prepend . $key] = $value;
141
            }
142
        }
143
144
        return $this->dotted[$cache] = $results;
145
    }
146
147
    /**
148
     * Flatten a nested array to a separated key.
149
     *
150
     * @param array       $array
151
     * @param string|null $separator
152
     * @param string      $prepend
153
     *
154
     * @return array
155
     */
156
    public function flatten(array $array, $separator = null, $prepend = '')
157
    {
158
        $flattened = [];
159
160 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...
161
            if (is_array($value)) {
162
                $flattened = array_merge($flattened, $this->flatten($value, $separator, $prepend . $key . $separator));
163
            } else {
164
                $flattened[$prepend . $key] = $value;
165
            }
166
        }
167
168
        return $flattened;
169
    }
170
}
171