Completed
Push — master ( ed33b0...7335c0 )
by Bocharsky
04:31
created

ArrayImitator::combineWith()   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

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
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 order by ASC
15
16
    /**
17
     * Add new element to the array.
18
     *
19
     * @param mixed $element
20
     *
21
     * @return $this
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 this array.
32
     *
33
     * @param int $size Size of each chunk
34
     * @param bool $preserveKeys Whether array keys are preserved or no
35
     *
36
     * @return static A new array of chunks from the original 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 array.
45
     *
46
     * @return $this An 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 this array as values and the other array as keys.
57
     *
58
     * @param array $array Key array
59
     * @deprecated Would be removed (or renamed)
60
     *
61
     * @return static A new array with keys from the other.
62
     */
63
    public function combineTo(array $array)
64
    {
65
        return new static(array_combine($array, $this->elements));
66
    }
67
68
    /**
69
     * Create an array using this array as keys and the other array as values.
70
     *
71
     * @param array $array Values array
72
     *
73
     * @return static A new array with values from the other array
74
     */
75 1
    public function combineWith(array $array)
76
    {
77 1
        return new static(array_combine($this->elements, $array));
78
    }
79
80
    /**
81
     * Compute the array of values not present in the other array.
82
     *
83
     * @param array $array Array for diff
84
     *
85
     * @return static A new array containing all the entries from this array
86
     * that are not present in $array
87
     */
88 4
    public function diffWith(array $array)
89
    {
90 4
        return new static(array_diff($this->elements, $array));
91
    }
92
93
    /**
94
     * Filter the array for elements satisfying the predicate $func.
95
     *
96
     * @param callable $func
97
     *
98
     * @return static A new array with only element satisfying $func
99
     */
100 4
    public function filter(callable $func)
101
    {
102 4
        return new static(array_filter($this->elements, $func));
103
    }
104
105
    /**
106
     * Exchanges all array keys with their associated values.
107
     *
108
     * @return static The new instance with flipped elements
109
     */
110 4
    public function flip()
111
    {
112 4
        return new static(array_flip($this->elements));
113
    }
114
115
    /**
116
     * Apply the given function to the every element of the array,
117
     * collecting the results.
118
     *
119
     * @param callable $func
120
     *
121
     * @return static A new array with modified elements
122
     */
123 4
    public function map(callable $func)
124
    {
125 4
        return new static(array_map($func, $this->elements));
126
    }
127
128
    /**
129
     * Merges array with the provided one. This array is overwriting.
130
     *
131
     * @param array $array Array to merge with (is overwritten)
132
     * @param bool $recursively Whether array will be merged recursively or no
133
     * @deprecated Would be removed (or renamed)
134
     *
135
     * @return static A new array with the keys/values
136
     * from $array added, that weren't present in the original
137
     */
138 View Code Duplication
    public function mergeTo(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...
139
    {
140
        if (true === $recursively) {
141
            return new static(array_merge_recursive($array, $this->elements));
142
        }
143
144
        return new static(array_merge($array, $this->elements));
145
    }
146
147
    /**
148
     * Merges this array with the provided one. Latter array is overwriting.
149
     *
150
     * @param array $array Array to merge with (overwrites)
151
     * @param bool $recursively Whether array will be merged recursively or no
152
     *
153
     * @return static A new array with the keys/values from $array added
154
     */
155 8 View Code Duplication
    public function mergeWith(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...
156
    {
157 8
        if (true === $recursively) {
158 4
            return new static(array_merge_recursive($this->elements, $array));
159
        }
160
161 4
        return new static(array_merge($this->elements, $array));
162
    }
163
164
    /**
165
     * Pad array to the specified size with a given value.
166
     *
167
     * @param int $size Size of the result array
168
     * @param mixed $value Empty value by default
169
     *
170
     * @return static A new array padded to $size with $value
171
     */
172 4
    public function pad($size, $value)
173
    {
174 4
        return new static(array_pad($this->elements, $size, $value));
175
    }
176
177
    /**
178
     * Create a numerically re-indexed array.
179
     *
180
     * @return static The new instance with re-indexed elements
181
     */
182 4
    public function reindex()
183
    {
184 4
        return new static(array_values($this->elements));
185
    }
186
187
    /**
188
     * Replace the entire array with the other one except keys present in both.
189
     * For keys present in both arrays the value from this array will be used.
190
     *
191
     * @param array $array Array to replace with
192
     * @param bool $recursively Whether array will be replaced recursively or no
193
     * @deprecated Would be removed (or renamed)
194
     *
195
     * @return static A new array with keys from $array and values from both.
196
     */
197 View Code Duplication
    public function replaceIn(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...
198
    {
199
        if (true === $recursively) {
200
            return new static(array_replace_recursive($array, $this->elements));
201
        }
202
203
        return new static(array_replace($array, $this->elements));
204
    }
205
206
    /**
207
     * Replace values in this array with values in the other array
208
     * that have the same key.
209
     *
210
     * @param array $array Array of replacing values
211
     * @param bool $recursively Whether array will be replaced recursively or no
212
     *
213
     * @return static A new array with the same keys but new values
214
     */
215 8 View Code Duplication
    public function replaceWith(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...
216
    {
217 8
        if (true === $recursively) {
218 4
            return new static(array_replace_recursive($this->elements, $array));
219
        }
220
221 4
        return new static(array_replace($this->elements, $array));
222
    }
223
224
    /**
225
     * Reverse the order of the array values.
226
     *
227
     * @param bool $preserveKeys Whether array keys are preserved or no
228
     *
229
     * @return static A new array with the order of the elements reversed
230
     */
231 4
    public function reverse($preserveKeys = false)
232
    {
233 4
        return new static(array_reverse($this->elements, $preserveKeys));
234
    }
235
236
    /**
237
     * Randomize element order.
238
     *
239
     * @return $this An array with the element order shuffled
240
     */
241 4
    public function shuffle()
242
    {
243 4
        shuffle($this->elements);
244
245 4
        return $this;
246
    }
247
248
    /**
249
     * Extract a slice of the array.
250
     *
251
     * @param int $offset Slice begin index
252
     * @param int|null $length Length of the slice
253
     * @param bool $preserveKeys Whether array keys are preserved or no
254
     *
255
     * @return static A slice of the original array with length $length
256
     */
257 4
    public function slice($offset, $length = null, $preserveKeys = false)
258
    {
259 4
        return new static(array_slice($this->elements, $offset, $length, $preserveKeys));
260
    }
261
262
    /**
263
     * Removes duplicate values from the array.
264
     *
265
     * @param int|null $sortFlags
266
     *
267
     * @return static A new array with only unique elements
268
     */
269 4
    public function unique($sortFlags = null)
270
    {
271 4
        return new static(array_unique($this->elements, $sortFlags));
272
    }
273
274
    /**
275
     * Apply the given function to every element in the array,
276
     * discarding the results.
277
     *
278
     * @param callable $func
279
     * @param bool $recursively Whether array will be walked recursively or no
280
     *
281
     * @return $this An array with modified elements
282
     */
283 8
    public function walk(callable $func, $recursively = false)
284
    {
285 8
        if (true === $recursively) {
286 4
            array_walk_recursive($this->elements, $func);
287 4
        } else {
288 4
            array_walk($this->elements, $func);
289
        }
290
291 8
        return $this;
292
    }
293
}
294