Skip to content

Commit e449a35

Browse files
committed
Added object oriented helper for convenient chaining Arr methods
1 parent c20aff4 commit e449a35

File tree

6 files changed

+886
-351
lines changed

6 files changed

+886
-351
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@
3030
"psr-4" : {
3131
"Minwork\\Helper\\" : "src/"
3232
}
33+
},
34+
"autoload-dev" : {
35+
"psr-4" : {
36+
"Test\\" : "test/"
37+
}
3338
}
3439
}

src/Arr.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,13 +757,16 @@ public static function sortObjects(array $objects, string $method, ...$args): ar
757757
/**
758758
* Sum associative arrays by their keys into one array
759759
*
760-
* @param array ...$arrays Can be either list of an arrays or single array of arrays
760+
* @param array $first Require first argument to ensure method is not called without arguments
761+
* @param array[] ...$arrays
761762
* @return array
762763
*/
763-
public static function sum(array ...$arrays): array
764+
public static function sum(array $first, array ...$arrays): array
764765
{
765766
$return = [];
766767

768+
array_unshift($arrays, $first);
769+
767770
foreach ($arrays as $array) {
768771
foreach ($array as $key => $value) {
769772
$return[$key] = ($return[$key] ?? 0) + floatval($value);
@@ -1141,4 +1144,15 @@ public static function getLastValue(array $array)
11411144

11421145
return $values[count($values) - 1];
11431146
}
1147+
1148+
/**
1149+
* Convenience method for creating new ArrObj instance
1150+
*
1151+
* @param array|ArrayAccess $array
1152+
* @return ArrObj
1153+
*/
1154+
public static function obj($array = []): ArrObj
1155+
{
1156+
return new ArrObj($array);
1157+
}
11441158
}

src/ArrObj.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)