1+ <?php
2+
3+ namespace Xdevor \ComposerParser \Concerns ;
4+
5+ use ArrayAccess ;
6+
7+ /**
8+ * The code here is based on/reference to Illuminate\Support/Arr.
9+ */
10+ trait ParseArray
11+ {
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+ public static function get ($ array , $ key , $ default = null )
21+ {
22+ if (! static ::accessible ($ array )) {
23+ return value ($ default );
24+ }
25+
26+ if (is_null ($ key )) {
27+ return $ array ;
28+ }
29+
30+ if (static ::exists ($ array , $ key )) {
31+ return $ array [$ key ];
32+ }
33+
34+ if (strpos ($ key , '. ' ) === false ) {
35+ return $ array [$ key ] ?? value ($ default );
36+ }
37+
38+ foreach (explode ('. ' , $ key ) as $ segment ) {
39+ if (static ::accessible ($ array ) && static ::exists ($ array , $ segment )) {
40+ $ array = $ array [$ segment ];
41+ } else {
42+ return value ($ default );
43+ }
44+ }
45+
46+ return $ array ;
47+ }
48+
49+ /**
50+ * Determine whether the given value is array accessible.
51+ *
52+ * @param mixed $value
53+ * @return bool
54+ */
55+ public static function accessible ($ 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+ public static function exists ($ array , $ key )
68+ {
69+ if ($ array instanceof ArrayAccess) {
70+ return $ array ->offsetExists ($ key );
71+ }
72+
73+ return array_key_exists ($ key , $ array );
74+ }
75+ }
0 commit comments