@@ -34,11 +34,12 @@ final class ArrayHelper
3434 *
3535 * @used-by dot_array_search()
3636 *
37- * @param string $index The index as dot array syntax.
37+ * @param string $index The index as dot array syntax.
38+ * @param array<array-key, mixed>|object $array
3839 *
39- * @return array|bool|int|object|string|null
40+ * @return array<array-key, mixed> |bool|int|object|string|null
4041 */
41- public static function dotSearch (string $ index , array $ array )
42+ public static function dotSearch (string $ index , array | object $ array )
4243 {
4344 return self ::arraySearchDot (self ::convertToArray ($ index ), $ array );
4445 }
@@ -78,9 +79,12 @@ private static function convertToArray(string $index): array
7879 *
7980 * @used-by dotSearch()
8081 *
81- * @return array|bool|float|int|object|string|null
82+ * @param list<string> $indexes
83+ * @param array<array-key, mixed>|object $array
84+ *
85+ * @return array<array-key, mixed>|bool|float|int|object|string|null
8286 */
83- private static function arraySearchDot (array $ indexes , array $ array )
87+ private static function arraySearchDot (array $ indexes , array | object $ array )
8488 {
8589 // If index is empty, returns null.
8690 if ($ indexes === []) {
@@ -90,7 +94,7 @@ private static function arraySearchDot(array $indexes, array $array)
9094 // Grab the current index
9195 $ currentIndex = array_shift ($ indexes );
9296
93- if (! isset ($ array[ $ currentIndex] ) && $ currentIndex !== '* ' ) {
97+ if (! self :: valueExists ($ array, $ currentIndex ) && $ currentIndex !== '* ' ) {
9498 return null ;
9599 }
96100
@@ -99,7 +103,7 @@ private static function arraySearchDot(array $indexes, array $array)
99103 $ answer = [];
100104
101105 foreach ($ array as $ value ) {
102- if (! is_array ($ value )) {
106+ if (! is_array ($ value ) && ! is_object ( $ value ) ) {
103107 return null ;
104108 }
105109
@@ -119,12 +123,14 @@ private static function arraySearchDot(array $indexes, array $array)
119123 // If this is the last index, make sure to return it now,
120124 // and not try to recurse through things.
121125 if ($ indexes === []) {
122- return $ array[ $ currentIndex] ;
126+ return self :: value ( $ array, $ currentIndex) ;
123127 }
124128
129+ $ value = self ::value ($ array , $ currentIndex );
130+
125131 // Do we need to recursively search this value?
126- if (is_array ($ array [ $ currentIndex ] ) && $ array [ $ currentIndex ] !== []) {
127- return self ::arraySearchDot ($ indexes , $ array [ $ currentIndex ] );
132+ if (( is_array ($ value ) && $ value !== []) || is_object ( $ value ) ) {
133+ return self ::arraySearchDot ($ indexes , $ value );
128134 }
129135
130136 // Otherwise, not found.
@@ -136,9 +142,9 @@ private static function arraySearchDot(array $indexes, array $array)
136142 *
137143 * If wildcard `*` is used, all items for the key after it must have the key.
138144 *
139- * @param array<array-key, mixed> $array
145+ * @param array<array-key, mixed>|object $array
140146 */
141- public static function dotHas (string $ index , array $ array ): bool
147+ public static function dotHas (string $ index , array | object $ array ): bool
142148 {
143149 self ::ensureValidWildcardPattern ($ index );
144150
@@ -154,10 +160,10 @@ public static function dotHas(string $index, array $array): bool
154160 /**
155161 * Recursively check key existence by dot path, including wildcard support.
156162 *
157- * @param array<array-key, mixed> $array
158- * @param list<string> $indexes
163+ * @param array<array-key, mixed>|object $array
164+ * @param list<string> $indexes
159165 */
160- private static function hasByDotPath (array $ array , array $ indexes ): bool
166+ private static function hasByDotPath (array | object $ array , array $ indexes ): bool
161167 {
162168 if ($ indexes === []) {
163169 return true ;
@@ -167,27 +173,29 @@ private static function hasByDotPath(array $array, array $indexes): bool
167173
168174 if ($ currentIndex === '* ' ) {
169175 foreach ($ array as $ item ) {
170- if (! is_array ($ item ) || ! self ::hasByDotPath ($ item , $ indexes )) {
176+ if (( ! is_array ($ item) && ! is_object ( $ item ) ) || ! self ::hasByDotPath ($ item , $ indexes )) {
171177 return false ;
172178 }
173179 }
174180
175181 return true ;
176182 }
177183
178- if (! array_key_exists ( $ currentIndex , $ array )) {
184+ if (! self :: keyExists ( $ array , $ currentIndex )) {
179185 return false ;
180186 }
181187
182188 if ($ indexes === []) {
183189 return true ;
184190 }
185191
186- if (! is_array ($ array [$ currentIndex ])) {
192+ $ value = self ::value ($ array , $ currentIndex );
193+
194+ if (! is_array ($ value ) && ! is_object ($ value )) {
187195 return false ;
188196 }
189197
190- return self ::hasByDotPath ($ array [ $ currentIndex ] , $ indexes );
198+ return self ::hasByDotPath ($ value , $ indexes );
191199 }
192200
193201 /**
@@ -333,13 +341,16 @@ public static function groupBy(array $array, array $indexes, bool $includeEmpty
333341
334342 /**
335343 * Recursively attach $row to the $indexes path of values found by
336- * `dot_array_search()` .
344+ * dot syntax .
337345 *
338346 * @used-by groupBy()
347+ *
348+ * @param array<array-key, mixed>|object $row
349+ * @param list<string> $indexes
339350 */
340351 private static function arrayAttachIndexedValue (
341352 array $ result ,
342- array $ row ,
353+ array | object $ row ,
343354 array $ indexes ,
344355 bool $ includeEmpty ,
345356 ): array {
@@ -349,7 +360,7 @@ private static function arrayAttachIndexedValue(
349360 return $ result ;
350361 }
351362
352- $ value = dot_array_search ($ index , $ row );
363+ $ value = self :: dotSearch ($ index , $ row );
353364
354365 if (! is_scalar ($ value )) {
355366 $ value = '' ;
@@ -447,6 +458,42 @@ public static function sortValuesByNatural(array &$array, $sortByIndex = null):
447458 });
448459 }
449460
461+ /**
462+ * @param array<array-key, mixed>|object $data
463+ */
464+ private static function keyExists (array |object $ data , string $ key ): bool
465+ {
466+ if (is_array ($ data )) {
467+ return array_key_exists ($ key , $ data );
468+ }
469+
470+ return array_key_exists ($ key , get_object_vars ($ data ));
471+ }
472+
473+ /**
474+ * @param array<array-key, mixed>|object $data
475+ */
476+ private static function valueExists (array |object $ data , string $ key ): bool
477+ {
478+ if (is_array ($ data )) {
479+ return isset ($ data [$ key ]);
480+ }
481+
482+ return isset (get_object_vars ($ data )[$ key ]);
483+ }
484+
485+ /**
486+ * @param array<array-key, mixed>|object $data
487+ */
488+ private static function value (array |object $ data , string $ key ): mixed
489+ {
490+ if (is_array ($ data )) {
491+ return $ data [$ key ];
492+ }
493+
494+ return get_object_vars ($ data )[$ key ];
495+ }
496+
450497 /**
451498 * Throws exception for invalid wildcard patterns.
452499 */
@@ -606,7 +653,7 @@ private static function projectByDotPath(
606653 $ currentIndex = array_shift ($ indexes );
607654
608655 if ($ currentIndex === '* ' ) {
609- if (! is_array ($ source )) {
656+ if (! is_array ($ source ) && ! is_object ( $ source ) ) {
610657 return ;
611658 }
612659
@@ -617,10 +664,10 @@ private static function projectByDotPath(
617664 return ;
618665 }
619666
620- if (! is_array ($ source ) || ! array_key_exists ( $ currentIndex , $ source )) {
667+ if (( ! is_array ($ source ) && ! is_object ( $ source )) || ! self :: keyExists ( $ source , $ currentIndex )) {
621668 return ;
622669 }
623670
624- self ::projectByDotPath ($ source[ $ currentIndex] , $ indexes , $ result , [...$ prefix , $ currentIndex ]);
671+ self ::projectByDotPath (self :: value ( $ source, $ currentIndex) , $ indexes , $ result , [...$ prefix , $ currentIndex ]);
625672 }
626673}
0 commit comments