Completed
Push — master ( 95445f...c30aee )
by Bocharsky
02:26
created

ArrayImitator::diff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 1
crap 1
1
<?php
2
3
namespace Arrayzy;
4
5
/**
6
 * Some methods could change the array instance itself
7
 * and some could return a new instance of ObjectOrientedArray.
8
 * This class repeats the PHP built-in functions behavior.
9
 *
10
 * @author Victor Bocharsky <[email protected]>
11
 */
12
class ArrayImitator extends AbstractArray
13
{
14
    // The public method list ordered by ASC
15
16
    /**
17
     * Add a new element to the current array.
18
     *
19
     * @param mixed $element
20
     *
21
     * @return ArrayImitator The current array with added value
22
     */
23 1
    public function add($element)
24
    {
25 1
        $this->elements[] = $element;
26
27 1
        return $this;
28
    }
29
30
    /**
31
     * Create a chunked version of current array.
32
     *
33
     * @param int $size Size of each chunk
34
     * @param bool $preserveKeys Whether array keys are preserved or no
35
     *
36
     * @return ArrayImitator A new array of chunks from the current array
37
     */
38 4
    public function chunk($size, $preserveKeys = false)
39
    {
40 4
        return new static(array_chunk($this->elements, $size, $preserveKeys));
41
    }
42
43
    /**
44
     * Clear the current array.
45
     *
46
     * @return ArrayImitator The current empty array
47
     */
48 4
    public function clear()
49
    {
50 4
        $this->elements = [];
51
52 4
        return $this;
53
    }
54
55
    /**
56
     * Create an array using the current array as keys and the other array as values.
57
     *
58
     * @param array $array Values array
59
     *
60
     * @return ArrayImitator A new array with values from the other array
61
     */
62 1
    public function combine(array $array)
63
    {
64 1
        return new static(array_combine($this->elements, $array));
65
    }
66
67
    /**
68
     * Compute the current array values which not present in the given one.
69
     *
70
     * @param array $array Array for diff
71
     *
72
     * @return ArrayImitator A new array containing all the entries from this array
73
     * that are not present in $array
74
     */
75 4
    public function diff(array $array)
76
    {
77 4
        return new static(array_diff($this->elements, $array));
78
    }
79
80
    /**
81
     * Filter the current array for elements satisfying the predicate $func.
82
     *
83
     * @param callable $func
84
     *
85
     * @return ArrayImitator A new array with only element satisfying $func
86
     */
87 4
    public function filter(callable $func)
88
    {
89 4
        return new static(array_filter($this->elements, $func));
90
    }
91
92
    /**
93
     * Exchanges all keys of current array with their associated values.
94
     *
95
     * @return ArrayImitator A new array with flipped elements
96
     */
97 4
    public function flip()
98
    {
99 4
        return new static(array_flip($this->elements));
100
    }
101
102
    /**
103
     * Apply the given function to the every element of the current array,
104
     * collecting the results.
105
     *
106
     * @param callable $func
107
     *
108
     * @return ArrayImitator A new array with modified elements
109
     */
110 4
    public function map(callable $func)
111
    {
112 4
        return new static(array_map($func, $this->elements));
113
    }
114
115
    /**
116
     * Merge the current array with the provided one. The latter array is overwriting.
117
     *
118
     * @param array $array Array to merge with (overwrites)
119
     * @param bool $recursively Whether array will be merged recursively or no
120
     *
121
     * @return ArrayImitator A new array with the keys/values from $array added
122
     */
123 8 View Code Duplication
    public function merge(array $array, $recursively = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
124
    {
125 8
        if (true === $recursively) {
126 4
            return new static(array_merge_recursive($this->elements, $array));
127
        }
128
129 4
        return new static(array_merge($this->elements, $array));
130
    }
131
132
    /**
133
     * Pad the current array to the specified size with a given value.
134
     *
135
     * @param int $size Size of the result array
136
     * @param mixed $value Empty value by default
137
     *
138
     * @return ArrayImitator A new array padded to $size with $value
139
     */
140 4
    public function pad($size, $value)
141
    {
142 4
        return new static(array_pad($this->elements, $size, $value));
143
    }
144
145
    /**
146
     * Create a numerically re-indexed array based on the current array.
147
     *
148
     * @return ArrayImitator A new array with re-indexed elements
149
     */
150 4
    public function reindex()
151
    {
152 4
        return new static(array_values($this->elements));
153
    }
154
155
    /**
156
     * Replace values in the current array with values in the given one
157
     * that have the same key.
158
     *
159
     * @param array $array Array of replacing values
160
     * @param bool $recursively Whether array will be replaced recursively or no
161
     *
162
     * @return ArrayImitator A new array with the same keys but new values
163
     */
164 8 View Code Duplication
    public function replace(array $array, $recursively = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
165
    {
166 8
        if (true === $recursively) {
167 4
            return new static(array_replace_recursive($this->elements, $array));
168
        }
169
170 4
        return new static(array_replace($this->elements, $array));
171
    }
172
173
    /**
174
     * Reverse the values order of the current array.
175
     *
176
     * @param bool $preserveKeys Whether array keys are preserved or no
177
     *
178
     * @return ArrayImitator A new array with the order of the elements reversed
179
     */
180 4
    public function reverse($preserveKeys = false)
181
    {
182 4
        return new static(array_reverse($this->elements, $preserveKeys));
183
    }
184
185
    /**
186
     * Randomize elements order of the current array.
187
     *
188
     * @return ArrayImitator The current array with the shuffled elements order
189
     */
190 4
    public function shuffle()
191
    {
192 4
        shuffle($this->elements);
193
194 4
        return $this;
195
    }
196
197
    /**
198
     * Extract a slice of the current array.
199
     *
200
     * @param int $offset Slice begin index
201
     * @param int|null $length Length of the slice
202
     * @param bool $preserveKeys Whether array keys are preserved or no
203
     *
204
     * @return ArrayImitator A new array, which is slice of the current array
205
     * with specified $length
206
     */
207 4
    public function slice($offset, $length = null, $preserveKeys = false)
208
    {
209 4
        return new static(array_slice($this->elements, $offset, $length, $preserveKeys));
210
    }
211
212
    /**
213
     * Remove duplicate values from the current array.
214
     *
215
     * @param int|null $sortFlags
216
     *
217
     * @return ArrayImitator A new array with only unique elements
218
     */
219 4
    public function unique($sortFlags = null)
220
    {
221 4
        return new static(array_unique($this->elements, $sortFlags));
222
    }
223
224
    /**
225
     * Apply the given function to the every element of the current array,
226
     * discarding the results.
227
     *
228
     * @param callable $func
229
     * @param bool $recursively Whether array will be walked recursively or no
230
     *
231
     * @return ArrayImitator The current array with modified elements
232
     */
233 8
    public function walk(callable $func, $recursively = false)
234
    {
235 8
        if (true === $recursively) {
236 4
            array_walk_recursive($this->elements, $func);
237 4
        } else {
238 4
            array_walk($this->elements, $func);
239
        }
240
241 8
        return $this;
242
    }
243
}
244