1<?php 2 3namespace dokuwiki\plugin\oauthosm; 4 5/** 6 * Dot notation access to arrays 7 * 8 * @see https://stackoverflow.com/a/39118759/172068 9 */ 10class DotAccess 11{ 12 13 /** 14 * Get an item from an array using "dot" notation. 15 * 16 * @param \ArrayAccess|array $array 17 * @param string $key 18 * @param mixed $default 19 * @return mixed 20 */ 21 public static function get($array, $key, $default = null) 22 { 23 if (!static::accessible($array)) { 24 return $default; 25 } 26 if (is_null($key)) { 27 return $array; 28 } 29 if (static::exists($array, $key)) { 30 return $array[$key]; 31 } 32 if (strpos($key, '.') === false) { 33 return $array[$key] ?? $default; 34 } 35 foreach (explode('.', $key) as $segment) { 36 if (static::accessible($array) && static::exists($array, $segment)) { 37 $array = $array[$segment]; 38 } else { 39 return $default; 40 } 41 } 42 return $array; 43 } 44 45 /** 46 * Determine whether the given value is array accessible. 47 * 48 * @param mixed $value 49 * @return bool 50 */ 51 protected static function accessible($value) 52 { 53 return is_array($value) || $value instanceof \ArrayAccess; 54 } 55 56 /** 57 * Determine if the given key exists in the provided array. 58 * 59 * @param \ArrayAccess|array $array 60 * @param string|int $key 61 * @return bool 62 */ 63 protected static function exists($array, $key) 64 { 65 if ($array instanceof \ArrayAccess) { 66 return $array->offsetExists($key); 67 } 68 return array_key_exists($key, $array); 69 } 70} 71