| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Honeybadger\Support; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class Arr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * Determine whether the given value is array accessible. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param  mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public static function accessible($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         return is_array($value) || $value instanceof \ArrayAccess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Get an item from an array using "dot" notation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param  \ArrayAccess|array  $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param  string  $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param  mixed   $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public static function get($array, $key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if (! static::accessible($array)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if (static::exists($array, $key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             return $array[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if (strpos($key, '.') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             return $array[$key] ?? $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         foreach (explode('.', $key) as $segment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             if (static::accessible($array) && static::exists($array, $segment)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $array = $array[$segment]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return $array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Determine if the given key exists in the provided array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param  \ArrayAccess|array  $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param  string|int  $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public static function exists($array, $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ($array instanceof \ArrayAccess) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return $array->offsetExists($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return array_key_exists($key, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param  array  $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param  callable  $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     public static function mapWithKeys(array $array, callable $callback) : array | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $newArray = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         foreach ($array as $key => $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             $newArray[$key] = $callback($item, $key); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         return $newArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * If the given value is not an array and not null, wrap it in one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param  mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public static function wrap($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (is_null($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return is_array($value) ? $value : [$value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |