Exemplo n.º 1
0
 /**
  * **Get an item from an array or object using "dot" notation.**
  *
  * @param  mixed        $target
  * @param  string|array $key
  * @param  mixed        $default
  *
  * @return mixed
  */
 public static function data_get($target, $key, $default = NULL)
 {
     if (NULL === $key) {
         return $target;
     }
     $key = is_array($key) ? $key : explode('.', $key);
     while (($segment = array_shift($key)) !== NULL) {
         if ($segment === '*') {
             if (is_object($target) and method_exists($target, 'toArray')) {
                 $target = $target->all();
             } elseif (!is_array($target)) {
                 return value($default);
             }
             $result = Lib::array_extract($target, $key);
             return in_array('*', $key, TRUE) ? Lib::collapse($result) : $result;
         }
         if (Lib::array_accessible($target) && Lib::key_exists($target, $segment)) {
             $target = $target[$segment];
         } elseif (is_object($target) && isset($target->{$segment})) {
             $target = $target->{$segment};
         } else {
             return value($default);
         }
     }
     return $target;
 }