Exemple #1
1
 /**
  * Get an item from an array or object using "dot" notation.
  *
  * @param  mixed   $target
  * @param  string|array  $key
  * @param  mixed   $default
  * @return mixed
  */
 function data_get($target, $key, $default = null)
 {
     if (is_null($key)) {
         return $target;
     }
     $key = is_array($key) ? $key : explode('.', $key);
     while (($segment = array_shift($key)) !== null) {
         if ($segment === '*') {
             if ($target instanceof Collection) {
                 $target = $target->all();
             } elseif (!is_array($target)) {
                 return value($default);
             }
             $result = Arr::pluck($target, $key);
             return in_array('*', $key) ? Arr::collapse($result) : $result;
         }
         if (Arr::accessible($target) && Arr::exists($target, $segment)) {
             $target = $target[$segment];
         } elseif (is_object($target) && isset($target->{$segment})) {
             $target = $target->{$segment};
         } else {
             return value($default);
         }
     }
     return $target;
 }
 public function locale($lang, Request $request)
 {
     if (Arr::exists(config('locales'), $lang)) {
         Session::set('locale', $lang);
     }
     if (isset($_SERVER['HTTP_REFERER'])) {
         return redirect('/')->setTargetUrl($_SERVER['HTTP_REFERER'])->withCookie('locale', $lang);
     } else {
         return redirect('/')->withCookie('locale', $lang);
     }
 }
 public function handle($request, \Closure $next)
 {
     // Load from cookie
     if (Cookie::has('locale')) {
         session(['locale' => Cookie::get('locale')]);
     }
     if (Session::has('locale') && Arr::exists(config('locales'), session('locale'))) {
         // Set app locale dynamically
         App::setLocale(session('locale'));
     }
     return $next($request);
 }
Exemple #4
0
 /**
  * Find a handler for manage a request
  *
  * @param  string $method
  * @param  string $path
  * @return null|callable
  */
 public function find($method, $path)
 {
     $method = Str::upper($method);
     $handlers = $this->handlers[$method];
     if (Arr::exists($handlers, $path)) {
         return $handlers[$path];
     }
     foreach ($handlers as $regexPath => $handler) {
         if (preg_match('@^' . $regexPath . '$@', $path)) {
             return $handler;
         }
     }
 }
Exemple #5
0
 public static function __callStatic($name, $attributes)
 {
     if (Str::startsWith('get', $name)) {
         $method = Str::camel($name);
         if (method_exists(new static(), $method)) {
             $class = static::class;
             if (Arr::exists($attributes, 0)) {
                 return $class::$method($attributes[0]);
             }
             return $class::$method();
         }
     }
     return env(Str::upper(Str::snake(Str::substr($name, 3))));
 }
Exemple #6
0
 /**
  * Rollback the last migration operation.
  *
  * @param array|string $paths
  * @param array        $options
  *
  * @return array
  */
 public function rollback($paths = [], array $options = [])
 {
     $this->notes = [];
     $rolledBack = [];
     $migrations = $this->getRanMigrations();
     $files = $this->getMigrationFiles($paths);
     $count = count($migrations);
     if ($count === 0) {
         $this->note('<info>Nothing to rollback.</info>');
     } else {
         $this->requireFiles($files);
         $steps = Arr::get($options, 'step', 0);
         if ($steps == 0) {
             $steps = 1;
         }
         $lastBatch = $this->repository->getLastBatchNumber();
         $stepDown = false;
         foreach ($migrations as $migration) {
             $migration = (object) $migration;
             if ($lastBatch > $migration->batch && $stepDown) {
                 $steps--;
                 $stepDown = false;
                 $lastBatch = $migration->batch;
             }
             if ($steps <= 0) {
                 break;
             }
             if (Arr::exists($files, $migration->migration)) {
                 $rolledBack[] = $files[$migration->migration];
                 $stepDown = true;
                 $this->runDown($files[$migration->migration], $migration, Arr::get($options, 'pretend', false));
             }
         }
     }
     return $rolledBack;
 }
 /**
  * Set an item on an array or object using dot notation.
  *
  * @param  mixed  $target
  * @param  string|array  $key
  * @param  mixed  $value
  * @param  bool  $overwrite
  * @return mixed
  */
 function data_set(&$target, $key, $value, $overwrite = true)
 {
     $segments = is_array($key) ? $key : explode('.', $key);
     if (($segment = array_shift($segments)) === '*') {
         if (!Arr::accessible($target)) {
             $target = [];
         }
         if ($segments) {
             foreach ($target as &$inner) {
                 data_set($inner, $segments, $value, $overwrite);
             }
         } elseif ($overwrite) {
             foreach ($target as &$inner) {
                 $inner = $value;
             }
         }
     } elseif (Arr::accessible($target)) {
         if ($segments) {
             if (!Arr::exists($target, $segment)) {
                 $target[$segment] = [];
             }
             data_set($target[$segment], $segments, $value, $overwrite);
         } elseif ($overwrite || !Arr::exists($target, $segment)) {
             $target[$segment] = $value;
         }
     } elseif (is_object($target)) {
         if ($segments) {
             if (!isset($target->{$segment})) {
                 $target->{$segment} = [];
             }
             data_set($target->{$segment}, $segments, $value, $overwrite);
         } elseif ($overwrite || !isset($target->{$segment})) {
             $target->{$segment} = $value;
         }
     } else {
         $target = [];
         if ($segments) {
             data_set($target[$segment], $segments, $value, $overwrite);
         } elseif ($overwrite) {
             $target[$segment] = $value;
         }
     }
     return $target;
 }
Exemple #8
0
 /**
  * {@inheritdoc}
  */
 public function exists($key)
 {
     $keys = is_array($key) ? $key : func_get_args();
     foreach ($keys as $value) {
         if (!Arr::exists($this->attributes, $value)) {
             return false;
         }
     }
     return true;
 }