1
+ <?php
2
+
3
+ namespace Minwork \Helper ;
4
+
5
+ use ArrayAccess ;
6
+ use BadMethodCallException ;
7
+
8
+ /**
9
+ * Class ArrObj
10
+ * @package Minwork\Helper
11
+ * @method bool has(mixed $keys)
12
+ * @method bool hasKeys(mixed $keys, bool $strict = false)
13
+ * @method mixed get(mixed $keys, $default = null)
14
+ * @method self set(mixed $keys, mixed $value)
15
+ * @method self remove(mixed $keys)
16
+ *
17
+ * @method bool check(mixed|callable $condition, bool $strict = false)
18
+ * @method bool isEmpty()
19
+ * @method bool isAssoc(bool $strict = false)
20
+ * @method bool isNumeric()
21
+ * @method bool isUnique(bool $strict = false)
22
+ * @method bool isNested()
23
+ * @method bool isArrayOfArrays()
24
+ *
25
+ * @method self map(callable $callback, int $mode = Arr::MAP_ARRAY_KEY_VALUE)
26
+ * @method self mapObjects(string $method, ...$args)
27
+ *
28
+ * @method self filterByKeys(mixed $keys, bool $exclude = false)
29
+ * @method self filterObjects(string $method, ...$args)
30
+ *
31
+ * @method self group(string|int $key)
32
+ * @method self groupObjects(string $method, ...$args)
33
+ *
34
+ * @method self orderByKeys(mixed $keys, bool $appendUnmatched = true)
35
+ * @method self sortByKeys(mixed $keys = null, bool $assoc = true)
36
+ * @method self sortObjects(string $method, ...$args)
37
+ *
38
+ * @method self sum(array ...$arrays)
39
+ * @method self diffObjects(array $array, array ...$arrays)
40
+ * @method self intersectObjects(array $array, array ...$arrays)
41
+ *
42
+ * @method self flatten(?int $depth = null, bool $assoc = false)
43
+ * @method self flattenSingle()
44
+ *
45
+ * @method int getDepth()
46
+ * @method self clone()
47
+ * @method mixed random(int $count = 1)
48
+ * @method self shuffle()
49
+ * @method self nth(int $A = 1, int $B = 0)
50
+ * @method self even()
51
+ * @method self odd()
52
+ *
53
+ * @method string|int|null getFirstKey()
54
+ * @method string|int|null getLastKey()
55
+ * @method mixed getFirstValue()
56
+ * @method mixed getLastValue()
57
+ */
58
+ class ArrObj
59
+ {
60
+ const METHODS = [
61
+ 'has ' ,
62
+ 'hasKeys ' ,
63
+ 'get ' ,
64
+ 'set ' ,
65
+ 'remove ' ,
66
+ 'check ' ,
67
+ 'isEmpty ' ,
68
+ 'isAssoc ' ,
69
+ 'isNumeric ' ,
70
+ 'isUnique ' ,
71
+ 'isNested ' ,
72
+ 'isArrayOfArrays ' ,
73
+ 'map ' ,
74
+ 'mapObjects ' ,
75
+ 'filterByKeys ' ,
76
+ 'filterObjects ' ,
77
+ 'group ' ,
78
+ 'groupObjects ' ,
79
+ 'orderByKeys ' ,
80
+ 'sortByKeys ' ,
81
+ 'sortObjects ' ,
82
+ 'sum ' ,
83
+ 'diffObjects ' ,
84
+ 'intersectObjects ' ,
85
+ 'flatten ' ,
86
+ 'flattenSingle ' ,
87
+ 'getDepth ' ,
88
+ 'clone ' ,
89
+ 'random ' ,
90
+ 'shuffle ' ,
91
+ 'nth ' ,
92
+ 'even ' ,
93
+ 'odd ' ,
94
+ 'getFirstKey ' ,
95
+ 'getLastKey ' ,
96
+ 'getFirstValue ' ,
97
+ 'getLastValue ' ,
98
+ ];
99
+
100
+ const CHAINABLE_METHODS = [
101
+ 'set ' ,
102
+ 'remove ' ,
103
+ 'map ' ,
104
+ 'mapObjects ' ,
105
+ 'filterByKeys ' ,
106
+ 'filterObjects ' ,
107
+ 'group ' ,
108
+ 'groupObjects ' ,
109
+ 'orderByKeys ' ,
110
+ 'sortByKeys ' ,
111
+ 'sortObjects ' ,
112
+ 'sum ' ,
113
+ 'diffObjects ' ,
114
+ 'intersectObjects ' ,
115
+ 'flatten ' ,
116
+ 'flattenSingle ' ,
117
+ 'clone ' ,
118
+ 'shuffle ' ,
119
+ 'nth ' ,
120
+ 'even ' ,
121
+ 'odd ' ,
122
+ ];
123
+
124
+ protected $ array ;
125
+
126
+ /**
127
+ * ArrObj constructor.
128
+ * @param array|ArrayAccess $array
129
+ */
130
+ public function __construct ($ array = [])
131
+ {
132
+ $ this ->setArray ($ array );
133
+ }
134
+
135
+ public function __call ($ name , $ arguments )
136
+ {
137
+ if (!in_array ($ name , self ::METHODS )) {
138
+ throw new BadMethodCallException ("Method {$ name } does not exists in Arr class or cannot be called on supplied array " );
139
+ }
140
+
141
+ $ result = Arr::$ name ($ this ->array , ...$ arguments );
142
+
143
+ if (in_array ($ name , self ::CHAINABLE_METHODS )) {
144
+ return $ this ->setArray ($ result );
145
+ }
146
+
147
+ return $ result ;
148
+ }
149
+
150
+ /**
151
+ * @return array|ArrayAccess
152
+ */
153
+ public function getArray ()
154
+ {
155
+ return $ this ->array ;
156
+ }
157
+
158
+ /**
159
+ * @param array|ArrayAccess $array
160
+ * @return ArrObj
161
+ */
162
+ public function setArray ($ array ): self
163
+ {
164
+ $ this ->array = $ array ;
165
+ return $ this ;
166
+ }
167
+ }
0 commit comments