Skip to content

Commit fb37516

Browse files
author
Harrison Ifeanyichukwu
committed
refactor: make array operation methods to accept array of string keys or a single string key
1 parent 84c9827 commit fb37516

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

src/Util.php

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,6 @@
88

99
class Util
1010
{
11-
/**
12-
* returns the value for the given array key if it exists, otherwise, return the default
13-
* value
14-
*
15-
*@param string $key - the array key
16-
*@param array $arr - the array
17-
*@param mixed [$default=null] - the default value to return if otherwise
18-
*@return mixed
19-
*/
20-
public static function value(string $key, array $arr, $default = null)
21-
{
22-
if (array_key_exists($key, $arr))
23-
return $arr[$key];
24-
else
25-
return $default;
26-
}
27-
28-
/**
29-
* returns array value for the given array key if key exists and its value is an array
30-
* otherwise, return the default value
31-
*
32-
*@param string $key - the array key
33-
*@param array $arr - the array
34-
*@param array [$default=[]] - the default value to return if otherwise
35-
*@return array
36-
*/
37-
public static function arrayValue(string $key, array $arr, array $default = [])
38-
{
39-
if (array_key_exists($key, $arr) && is_array($arr[$key]))
40-
return $arr[$key];
41-
else
42-
return $default;
43-
}
44-
4511
/**
4612
* returns true if the given key is not set or if the key is set and it is truthy
4713
*
@@ -86,18 +52,6 @@ public static function isNumeric($value)
8652
return false;
8753
}
8854

89-
/**
90-
* unsets an entry from an array if the key exists
91-
*
92-
*@param string $key - the array key
93-
*@param array $arr - the array
94-
*/
95-
public static function unsetFromArray(string $key, array &$arr)
96-
{
97-
if (array_key_exists($key, $arr))
98-
unset($arr[$key]);
99-
}
100-
10155
/**
10256
* puts the value inside an array and returns the resulting array or returns the value if
10357
* it is already an array
@@ -112,4 +66,63 @@ public static function makeArray($value)
11266
else
11367
return array($value);
11468
}
69+
70+
/**
71+
* returns the value for the first key in the keys array that exists in the array
72+
* otherwise, return the default value
73+
*
74+
*@param string[]|string $keys - array of keys or a single string key
75+
*@param array $arr - the array
76+
*@param mixed [$default=null] - the default value to return if otherwise
77+
*@return mixed
78+
*/
79+
public static function value($keys, array $arr, $default = null)
80+
{
81+
$keys = self::makeArray($keys);
82+
foreach($keys as $key)
83+
{
84+
if (array_key_exists($key, $arr))
85+
return $arr[$key];
86+
}
87+
88+
return $default;
89+
}
90+
91+
/**
92+
* returns the value for the first key in the keys array whose value is an array that
93+
* exists in the array
94+
* otherwise, return the default value
95+
*
96+
*@param string[]|string $keys - array of keys or a single string key
97+
*@param array $arr - the array
98+
*@param mixed [$default=[]] - the default value to return if otherwise
99+
*@return array
100+
*/
101+
public static function arrayValue($keys, array $arr, array $default = [])
102+
{
103+
$keys = self::makeArray($keys);
104+
foreach($keys as $key)
105+
{
106+
if (array_key_exists($key, $arr) && is_array($arr[$key]))
107+
return $arr[$key];
108+
}
109+
110+
return $default;
111+
}
112+
113+
/**
114+
* unsets array of keys from the given array if it exists in the array
115+
*
116+
*@param string[]|string $keys - array of keys or a single string key
117+
*@param array $arr - the array
118+
*/
119+
public static function unsetFromArray($keys, array &$arr)
120+
{
121+
$keys = self::makeArray($keys);
122+
foreach($keys as $key)
123+
{
124+
if (array_key_exists($key, $arr))
125+
unset($arr[$key]);
126+
}
127+
}
115128
}

0 commit comments

Comments
 (0)