22
33namespace Xdevor \ComposerParser \Concerns ;
44
5- use ArrayAccess ;
6-
7- /**
8- * The code here is based on/reference to Illuminate\Support/Arr.
9- */
105trait ParseArray
116{
12- /**
13- * Get an item from an array using "dot" notation.
14- *
15- * @param \ArrayAccess|array $array
16- * @param string|int|null $key
17- * @param mixed $default
18- * @return mixed
19- */
20- private static function parseArray ($ array , $ key , $ default = null )
7+ private static function parseArray (array $ array , string $ key , $ default = null )
218 {
22- if (! static ::isValueAccessible ($ array )) {
23- return value ($ default );
24- }
25-
26- if (is_null ($ key )) {
27- return $ array ;
28- }
29-
30- if (static ::isKeyExists ($ array , $ key )) {
9+ if (array_key_exists ($ key , $ array )) {
3110 return $ array [$ key ];
3211 }
3312
3413 if (strpos ($ key , '. ' ) === false ) {
35- return $ array [$ key ] ?? value ( $ default) ;
14+ return $ array [$ key ] ?? $ default ;
3615 }
3716
3817 foreach (explode ('. ' , $ key ) as $ segment ) {
39- if (static :: isValueAccessible ( $ array ) && static :: isKeyExists ( $ array , $ segment )) {
18+ if (array_key_exists ( $ segment , $ array )) {
4019 $ array = $ array [$ segment ];
4120 } else {
42- return value ( $ default) ;
21+ return $ default ;
4322 }
4423 }
4524
4625 return $ array ;
4726 }
48-
49- /**
50- * Determine whether the given value is array accessible.
51- *
52- * @param mixed $value
53- * @return bool
54- */
55- private static function isValueAccessible ($ value )
56- {
57- return is_array ($ value ) || $ value instanceof ArrayAccess;
58- }
59-
60- /**
61- * Determine if the given key exists in the provided array.
62- *
63- * @param \ArrayAccess|array $array
64- * @param string|int $key
65- * @return bool
66- */
67- private static function isKeyExists ($ array , $ key )
68- {
69- if ($ array instanceof ArrayAccess) {
70- return $ array ->offsetExists ($ key );
71- }
72-
73- return array_key_exists ($ key , $ array );
74- }
7527}
0 commit comments