Skip to content

Commit 40e8b31

Browse files
committed
Added new common methods and refactored PHPDoc
1 parent bb06a81 commit 40e8b31

File tree

2 files changed

+210
-22
lines changed

2 files changed

+210
-22
lines changed

src/Arr.php

Lines changed: 101 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,41 @@ public static function getKeysArray($keys): array
8080
}
8181

8282
/**
83-
* Check if array has specified keys
83+
* Check if specified (nested) key(s) exists in array
8484
*
8585
* @param array $array
86-
* @param mixed $keys
87-
* See getKeysArray method
88-
* @param bool $strict
89-
* If array must have all of specified keys
86+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
9087
* @return bool
91-
* @see \Minwork\Helper\Arr::getKeysArray()
88+
* @see Arr::getKeysArray()
89+
*/
90+
public static function has(array $array, $keys): bool
91+
{
92+
$keysArray = self::getKeysArray($keys);
93+
94+
if (empty($keysArray)) {
95+
return false;
96+
}
97+
98+
$tmp = $array;
99+
100+
foreach ($keysArray as $key) {
101+
if (!array_key_exists($key, $tmp)) {
102+
return false;
103+
}
104+
$tmp = $tmp[$key];
105+
}
106+
107+
return true;
108+
}
109+
110+
/**
111+
* Check if array has list of specified keys
112+
*
113+
* @param array $array
114+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
115+
* @param bool $strict If array must have all of specified keys
116+
* @return bool
117+
* @see Arr::getKeysArray()
92118
*/
93119
public static function hasKeys(array $array, $keys, bool $strict = false): bool
94120
{
@@ -102,17 +128,28 @@ public static function hasKeys(array $array, $keys, bool $strict = false): bool
102128
return $strict ? true : false;
103129
}
104130

131+
/**
132+
* Alias of Arr::getNestedElement
133+
*
134+
* @param array|ArrayAccess $array Array or object implementing array access to get element from
135+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
136+
* @param mixed $default Default value if element was not found
137+
* @return null|mixed
138+
* @see Arr::getNestedElement()
139+
*/
140+
public static function get($array, $keys, $default = null)
141+
{
142+
return self::getNestedElement($array, $keys, $default);
143+
}
144+
105145
/**
106146
* Get nested element of an array or object implementing array access
107147
*
108-
* @param array|ArrayAccess $array
109-
* Array or object implementing array access to get element from
110-
* @param mixed $keys
111-
* See getKeysArray method
112-
* @param mixed $default
113-
* Default value if element was not found
148+
* @param array|ArrayAccess $array Array or object implementing array access to get element from
149+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
150+
* @param mixed $default Default value if element was not found
114151
* @return null|mixed
115-
* @see \Minwork\Helper\Arr::getKeysArray()
152+
* @see Arr::getKeysArray()
116153
*/
117154
public static function getNestedElement($array, $keys, $default = null)
118155
{
@@ -130,15 +167,28 @@ public static function getNestedElement($array, $keys, $default = null)
130167
return $array;
131168
}
132169

170+
/**
171+
* Alias of Arr::setNestedElement
172+
*
173+
* @param array $array
174+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
175+
* @param mixed $value Value to set
176+
* @return array Copy of an array with element set
177+
* @see Arr::setNestedElement()
178+
*/
179+
public static function set(array $array, $keys, $value): array
180+
{
181+
return self::setNestedElement($array, $keys, $value);
182+
}
183+
133184
/**
134185
* Set array element specified by keys to the desired value (create missing keys if necessary)
135186
*
136187
* @param array $array
137188
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
138189
* @param mixed $value Value to set
139190
* @return array Copy of an array with element set
140-
* @see \Minwork\Helper\Arr::getKeysArray
141-
* @see \Minwork\Helper\Arr::getKeysArray()
191+
* @see Arr::getKeysArray()
142192
*/
143193
public static function setNestedElement(array $array, $keys, $value): array
144194
{
@@ -163,6 +213,35 @@ public static function setNestedElement(array $array, $keys, $value): array
163213
return $result;
164214
}
165215

216+
/**
217+
* Destroy variable inside array at path specified by keys
218+
*
219+
* @param array $array
220+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
221+
* @return array
222+
* @see Arr::getKeysArray()
223+
*/
224+
public static function unset(array $array, $keys): array
225+
{
226+
$result = self::clone($array);
227+
$keysArray = self::getKeysArray($keys);
228+
229+
$tmp = &$result;
230+
231+
while (count($keysArray) > 1) {
232+
$key = array_shift($keysArray);
233+
if (!is_array($tmp) || !array_key_exists($key, $tmp)) {
234+
return $result;
235+
}
236+
237+
$tmp = &$tmp[$key];
238+
}
239+
$key = array_shift($keysArray);
240+
unset($tmp[$key]);
241+
242+
return $result;
243+
}
244+
166245
/**
167246
* Converts map of keys concatenated by dot and corresponding values to multidimensional array
168247
*
@@ -468,10 +547,10 @@ public static function mapObjects(array $objects, string $method, ...$args): arr
468547
* Filter array values by preserving only those which keys are present in array obtained from $keys variable
469548
*
470549
* @param array $array
471-
* @param mixed $keys See getKeysArray function
550+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
472551
* @param bool $exclude If values matching $keys should be excluded from returned array
473552
* @return array
474-
* @see \Minwork\Helper\Arr::getKeysArray()
553+
* @see Arr::getKeysArray()
475554
*/
476555
public static function filterByKeys(array $array, $keys, bool $exclude = false): array
477556
{
@@ -592,10 +671,10 @@ public static function groupObjects(array $objects, string $method, ...$args): a
592671
* Order associative array according to supplied keys order
593672
* Keys that are not present in $keys param will be appended to the end of an array preserving supplied order.
594673
* @param array $array
595-
* @param mixed $keys See getKeysArray method
674+
* @param mixed $keys Keys needed to access desired array element (for possible formats see getKeysArray method)
596675
* @param bool $appendUnmatched If values not matched by supplied keys should be appended to the end of an array
597676
* @return array
598-
* @see \Minwork\Helper\Arr::getKeysArray()
677+
* @see Arr::getKeysArray()
599678
*/
600679
public static function orderByKeys(array $array, $keys, bool $appendUnmatched = true): array
601680
{
@@ -617,7 +696,7 @@ public static function orderByKeys(array $array, $keys, bool $appendUnmatched =
617696
* @param mixed $keys Keys in format specified by getKeysArray method or null to perform sort using 0-depth keys
618697
* @param bool $assoc If sorting should preserve main array keys (default: true)
619698
* @return array New sorted array
620-
* @see \Minwork\Helper\Arr::getKeysArray()
699+
* @see Arr::getKeysArray()
621700
*/
622701
public static function sortByKeys(array $array, $keys = null, bool $assoc = true): array
623702
{
@@ -944,8 +1023,8 @@ public static function shuffle(array $array): array
9441023
* @param int $A
9451024
* @param int $B
9461025
* @return array
947-
* @see \Minwork\Helper\Arr::even()
948-
* @see \Minwork\Helper\Arr::odd()
1026+
* @see Arr::even()
1027+
* @see Arr::odd()
9491028
*/
9501029
public static function nth(array $array, int $A = 1, int $B = 0): array
9511030
{

test/ArrTest.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@ public function testGetKeysArray()
3838
$this->assertSame([], Arr::getKeysArray(null));
3939
}
4040

41+
public function testHas()
42+
{
43+
$array = [
44+
'test' => [
45+
'test1',
46+
'test2' => [
47+
'test3' => 'abc',
48+
'test4'
49+
],
50+
[
51+
'test6' => 'def'
52+
],
53+
],
54+
];
55+
56+
$this->assertTrue(Arr::has($array, 'test'));
57+
$this->assertTrue(Arr::has($array, 'test.test2'));
58+
$this->assertTrue(Arr::has($array, 'test.test2.test3'));
59+
$this->assertTrue(Arr::has($array, 'test.0'));
60+
$this->assertTrue(Arr::has($array, 'test.1.test6'));
61+
$this->assertTrue(Arr::has($array, ['test', 1, 'test6']));
62+
63+
$this->assertFalse(Arr::has($array, []));
64+
$this->assertFalse(Arr::has($array, new stdClass()));
65+
$this->assertFalse(Arr::has($array, 0));
66+
$this->assertFalse(Arr::has($array, 'test2'));
67+
$this->assertFalse(Arr::has($array, 'test.test1'));
68+
$this->assertFalse(Arr::has($array, 'test.test2.test4'));
69+
}
70+
4171
public function testHasKeys()
4272
{
4373
$array = ['key1' => 1, 'key2' => 2, 'key3' => 3];
@@ -73,6 +103,13 @@ public function testGetNestedElement()
73103
$this->assertSame('test', Arr::getNestedElement($object, 'key'));
74104
$this->assertNull(Arr::getNestedElement($object, 'key2'));
75105
$this->assertSame('test2', Arr::getNestedElement($object, 'nested.key'));
106+
107+
// Test alias
108+
$this->assertSame(Arr::getNestedElement($array, 'key1.key2.key3'), Arr::get($array, 'key1.key2.key3'));
109+
$this->assertSame(Arr::getNestedElement($array, 'key1.key2.key3', 'default'), Arr::get($array, 'key1.key2.key3', 'default'));
110+
/** @noinspection PhpParamsInspection */
111+
$this->assertSame(Arr::getNestedElement(new stdClass(), 'key1.key4.key2.key3', 'default'), Arr::get(new stdClass(), 'key1.key4.key2.key3', 'default'));
112+
$this->assertSame(Arr::getNestedElement($object, 'nested.key'), Arr::get($object, 'nested.key'));
76113
}
77114

78115
public function testSetNestedElement()
@@ -110,6 +147,78 @@ public function testSetNestedElement()
110147

111148
$this->assertSame([[['test']]], Arr::setNestedElement([], '[].[].[]', 'test'));
112149
$this->assertSame([[[[]]]], Arr::setNestedElement([], '[].[].[]', []));
150+
151+
// Test alias
152+
$this->assertSame(Arr::setNestedElement([], '[].[].[]', 'test'), Arr::set([], '[].[].[]', 'test'));
153+
$this->assertSame(Arr::setNestedElement($array, 'test.test2.test3', 'abc'), Arr::set($array, 'test.test2.test3', 'abc'));
154+
$this->assertSame(Arr::setNestedElement($array, 'key1.key2', ['key3' => 'test']), Arr::set($array, 'key1.key2', ['key3' => 'test']));
155+
}
156+
157+
public function testUnset()
158+
{
159+
$array = [
160+
'test' => [
161+
'test1',
162+
'test2' => [
163+
'test3' => 'abc',
164+
'test4'
165+
],
166+
[
167+
'test6' => 'def'
168+
],
169+
],
170+
];
171+
172+
$this->assertSame([], Arr::unset($array, 'test'));
173+
$this->assertSame([
174+
'test' => [
175+
'test1',
176+
[
177+
'test6' => 'def'
178+
],
179+
],
180+
], Arr::unset($array, 'test.test2'));
181+
182+
$this->assertSame([
183+
'test' => [
184+
'test2' => [
185+
'test3' => 'abc',
186+
'test4'
187+
],
188+
1 => [
189+
'test6' => 'def'
190+
],
191+
]
192+
], Arr::unset($array, 'test.0'));
193+
194+
$this->assertSame([
195+
'test' => [
196+
'test1',
197+
'test2' => [
198+
'test4'
199+
],
200+
[
201+
'test6' => 'def'
202+
],
203+
],
204+
], Arr::unset($array, 'test.test2.test3'));
205+
206+
$this->assertSame([
207+
'test' => [
208+
'test1',
209+
'test2' => [
210+
'test3' => 'abc',
211+
'test4'
212+
],
213+
[],
214+
],
215+
], Arr::unset($array, 'test.1.test6'));
216+
217+
$this->assertSame($array, Arr::unset($array, '0'));
218+
$this->assertSame($array, Arr::unset($array, 'test.test1'));
219+
$this->assertSame($array, Arr::unset($array, 'test.test2.test4'));
220+
$this->assertSame($array, Arr::unset($array, 'test.test2.test4.test5.test6.abc'));
221+
$this->assertSame($array, Arr::unset($array, 'test.2'));
113222
}
114223

115224
public function testPack()

0 commit comments

Comments
 (0)