-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayUtils.php
More file actions
123 lines (109 loc) · 2.45 KB
/
Copy pathArrayUtils.php
File metadata and controls
123 lines (109 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Amitmerchant\ArrayUtils;
use Closure;
class ArrayUtils
{
private $collection;
/**
* Returns the class instance
*
* @return \AmitMerchant\ArrayUtils\ArrayUtils
*/
public static function getInstance(): ArrayUtils
{
return new ArrayUtils();
}
/**
* Collects the input array
*
* @param array $collection
* @return $this
*/
public function collect(array $collection)
{
$this->collection = $collection;
return $this;
}
/**
* Wrapper method for array_map
*
* @param Closure $closure
* @return array
*/
public function map(Closure $closure): array
{
return array_map($closure, $this->collection);
}
/**
* Wrapper method for array_filter
*
* @param Closure $closure
* @return array
*/
public function filter(Closure $closure): array
{
return array_filter($this->collection, $closure);
}
/**
* Wrapper method for in_array
*
* @param mixed $item
* @return bool
*/
public function contains(mixed $item): bool
{
return in_array($item, $this->collection);
}
/**
* Wrapper method for array_values
*
* @return array
*/
public function getValues(): array
{
return array_values($this->collection);
}
/**
* Wrapper method for array_keys
*
* @return array
*/
public function getKeys(): array
{
return array_keys($this->collection);
}
/**
* Wrapper method for array_search
*
* @param mixed $searchParam
* @return false|int|string
*/
public function search(mixed $searchParam, bool $strict = false): int|string|false
{
return array_search($searchParam, $this->collection, $strict = false);
}
/**
* Wrapper method for array_reduce
*
* @param Closure $callback
* @param mixed $initial
*
* @return mixed
*/
public function reduce(Closure $callback, mixed $initial = null): mixed
{
return array_reduce($this->collection, $callback, $initial);
}
/**
* Wrapper method for array_chunk
*
* @param int $length
* @param bool $preserve_keys
*
* @return array
*/
public function chunk(int $length, bool $preserve_keys = false): array
{
return array_chunk($this->collection, $length, $preserve_keys);
}
}