11
11
12
12
use ArrayAccess ;
13
13
use Toolkit \Stdlib \Php ;
14
- use Traversable ;
15
14
use function array_filter ;
16
15
use function array_shift ;
17
16
use function count ;
@@ -32,9 +31,9 @@ trait ArrayValueGetSetTrait
32
31
/**
33
32
* Add an element to an array using "dot" notation if it doesn't exist.
34
33
*
35
- * @param array $array
34
+ * @param array $array
36
35
* @param string $key
37
- * @param mixed $value
36
+ * @param mixed $value
38
37
*
39
38
* @return array
40
39
*/
@@ -81,9 +80,9 @@ public static function get(ArrayAccess|array $array, string $key, mixed $default
81
80
* Set an array item to a given value using "dot" notation.
82
81
* If no key is given to the method, the entire array will be replaced.
83
82
*
84
- * @param array $array
83
+ * @param array $array
85
84
* @param string $key
86
- * @param mixed $value
85
+ * @param mixed $value
87
86
*
88
87
* @return array
89
88
*/
@@ -114,9 +113,8 @@ public static function set(array &$array, string $key, mixed $value): array
114
113
/**
115
114
* Get Multi - 获取多个, 可以设置默认值
116
115
*
117
- * @param array $data array data
118
- * @param array $needKeys
119
- * $needKeys = [
116
+ * @param array $data array data
117
+ * @param array $needKeys = [
120
118
* 'name',
121
119
* 'password',
122
120
* 'status' => '1'
@@ -131,7 +129,7 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
131
129
132
130
foreach ($ needKeys as $ key => $ default ) {
133
131
if (is_int ($ key )) {
134
- $ key = $ default ;
132
+ $ key = $ default ;
135
133
$ default = null ;
136
134
}
137
135
@@ -163,14 +161,14 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
163
161
* Get data from array or object by path.
164
162
* Example: `DataCollector::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo'].
165
163
*
166
- * @param Traversable|array $data An array or object to get value.
167
- * @param string $path The key path.
164
+ * @param iterable $data An array or object to get value.
165
+ * @param string $path The key path.
168
166
* @param mixed|null $default
169
- * @param string $separator Separator of paths.
167
+ * @param string $separator Separator of paths.
170
168
*
171
169
* @return mixed Found value, null if not exists.
172
170
*/
173
- public static function getByPath (Traversable | array $ data , string $ path , mixed $ default = null , string $ separator = '. ' ): mixed
171
+ public static function getByPath (iterable $ data , string $ path , mixed $ default = null , string $ separator = '. ' ): mixed
174
172
{
175
173
if (isset ($ data [$ path ])) {
176
174
return $ data [$ path ];
@@ -224,12 +222,12 @@ public static function getValueByNodes(array $data, array $nodes, mixed $default
224
222
/**
225
223
* setByPath
226
224
*
227
- * @param ArrayAccess| array & $data
228
- * @param string $path
229
- * @param mixed $value
230
- * @param string $separator
225
+ * @param array $data
226
+ * @param string $path
227
+ * @param mixed $value
228
+ * @param string $separator
231
229
*/
232
- public static function setByPath (ArrayAccess | array &$ data , string $ path , mixed $ value , string $ separator = '. ' ): void
230
+ public static function setByPath (array &$ data , string $ path , mixed $ value , string $ separator = '. ' ): void
233
231
{
234
232
if (!str_contains ($ path , $ separator )) {
235
233
$ data [$ path ] = $ value ;
0 commit comments