1
|
|
|
<?php |
2
|
|
|
namespace Narrowspark\Arr; |
3
|
|
|
|
4
|
|
|
use Narrowspark\Arr\Traits\ValueTrait; |
5
|
|
|
|
6
|
|
|
class Traverse |
7
|
|
|
{ |
8
|
|
|
use ValueTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Applies the callback to the elements of the given arrays |
12
|
|
|
* |
13
|
|
|
* @param array $array |
14
|
|
|
* @param callable $callback |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
public function map(array $array, callable $callback) |
19
|
|
|
{ |
20
|
|
|
$newArray = []; |
21
|
|
|
|
22
|
|
|
foreach ($array as $key => $item) { |
23
|
|
|
$result = call_user_func($callback, $item, $key); |
24
|
|
|
|
25
|
|
|
$newArray = is_array($result) ? |
26
|
|
|
array_replace_recursive($array, $result) : |
27
|
|
|
array_merge_recursive($array, (array) $result); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $newArray; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Filters each of the given values through a function. |
35
|
|
|
* |
36
|
|
|
* @param array $array |
37
|
|
|
* @param callable $callback |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function filter(array $array, callable $callback) |
42
|
|
|
{ |
43
|
|
|
$newArray = []; |
44
|
|
|
|
45
|
|
|
foreach ($array as $key => $item) { |
46
|
|
|
if (call_user_func($callback, $item, $key)) { |
47
|
|
|
$newArray[$key] = $item; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $newArray; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns whether every element of the array satisfies the given predicate or not. |
56
|
|
|
* Works with Iterators too. |
57
|
|
|
* |
58
|
|
|
* @param array $array |
59
|
|
|
* @param callable $predicate |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function all(array $array, callable $predicate) |
64
|
|
|
{ |
65
|
|
|
foreach ($array as $key => $value) { |
66
|
|
|
if (!call_user_func($predicate, $value, $key, $array)) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The opposite of filter(). |
76
|
|
|
* |
77
|
|
|
* @param array $array |
78
|
|
|
* @param callable $callback Function to filter values. |
79
|
|
|
* |
80
|
|
|
* @return array filtered array. |
81
|
|
|
*/ |
82
|
|
|
public function reject(array $array, callable $callback) |
83
|
|
|
{ |
84
|
|
|
return $this->filter($array, function ($value, $key) use ($callback) { |
85
|
|
|
return !call_user_func($callback, $value, $key); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Filter the array using the given Closure. |
91
|
|
|
* |
92
|
|
|
* @param array $array |
93
|
|
|
* @param callable $callback |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function where(array $array, callable $callback) |
98
|
|
|
{ |
99
|
|
|
$filtered = []; |
100
|
|
|
|
101
|
|
|
foreach ($array as $key => $value) { |
102
|
|
|
if (call_user_func($callback, $key, $value)) { |
103
|
|
|
$filtered[$key] = $value; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $filtered; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the first element in an array passing a given truth test. |
112
|
|
|
* |
113
|
|
|
* @param array $array |
114
|
|
|
* @param callable $callback |
115
|
|
|
* @param mixed $default |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function first(array $array, callable $callback, $default = null) |
120
|
|
|
{ |
121
|
|
|
foreach ($array as $key => $value) { |
122
|
|
|
if (call_user_func($callback, $key, $value)) { |
123
|
|
|
return $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->value($default); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return the last element in an array passing a given truth test. |
132
|
|
|
* |
133
|
|
|
* @param array $array |
134
|
|
|
* @param callable $callback |
135
|
|
|
* @param mixed $default |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
|
|
public function last(array $array, callable $callback, $default = null) |
140
|
|
|
{ |
141
|
|
|
return $this->first(array_reverse($array), $callback, $default); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|