22
33namespace Selective \ArrayReader ;
44
5+ use Cake \Chronos \Chronos ;
56use Exception ;
67use InvalidArgumentException ;
78
@@ -269,23 +270,18 @@ public function findChronos(string $key, Chronos $default = null)
269270 */
270271 public function find (string $ path , $ default = null )
271272 {
272- if (strpos ($ path , '. ' ) === false ) {
273- return $ this ->data [$ path ] ?? $ default ;
274- }
273+ $ pathKeys = explode ('. ' , $ path );
275274
276- $ parts = explode ('. ' , $ path );
277- $ value = &$ this ->data ;
275+ $ arrayCopyOrValue = $ this ->data ;
278276
279- foreach ($ parts as $ key ) {
280- if (isset ($ value [$ key ])) {
281- $ value = &$ value [$ key ];
282- } else {
277+ foreach ($ pathKeys as $ pathKey ) {
278+ if (!isset ($ arrayCopyOrValue [$ pathKey ])) {
283279 return $ default ;
284280 }
281+ $ arrayCopyOrValue = $ arrayCopyOrValue [$ pathKey ];
285282 }
286- $ value = &$ this ->$ value ;
287283
288- return $ value ;
284+ return $ arrayCopyOrValue ;
289285 }
290286
291287 /**
@@ -311,19 +307,15 @@ public function all(): array
311307 */
312308 public function exists (string $ path ): bool
313309 {
314- if (strpos ($ path , '. ' ) === false ) {
315- return array_key_exists ($ path , $ this ->data );
316- }
310+ $ pathKeys = explode ('. ' , $ path );
317311
318- $ parts = explode ( ' . ' , $ path ) ;
312+ $ arrayCopyOrValue = $ this -> data ;
319313
320- $ temp = &$ this ->data ;
321- foreach ($ parts as $ key ) {
322- if (array_key_exists ($ key , $ temp )) {
323- $ temp = &$ this ->data [$ key ];
324- } else {
314+ foreach ($ pathKeys as $ pathKey ) {
315+ if (!array_key_exists ($ pathKey , $ arrayCopyOrValue )) {
325316 return false ;
326317 }
318+ $ arrayCopyOrValue = $ arrayCopyOrValue [$ pathKey ];
327319 }
328320
329321 return true ;
0 commit comments