/** * search getter * * @param string $siteKey site key * @param string $name the name * @return ConfigEntity */ public function find($siteKey, $name) { $data = $this->getData($siteKey, $this->getHead($name)); return Arr::first($data, function ($idx, $item) use($name) { return $item->name === $name; }); }
public function handle(Request $request, Closure $next) { $route = $request->route(); $parameters = $route->parameters(); $parameterKeys = array_keys($parameters); // Look for parameters that match {*_morph_type} and {*_morph_id} $morphTypeName = Arr::first($parameterKeys, function ($item) { return ends_with($item, '_type'); }); $morphIdName = Arr::first($parameterKeys, function ($item) { return ends_with($item, '_id'); }); if (!$morphTypeName or !$morphIdName) { return $next($request); } $morphKey = preg_replace("/_type\$/", '', $morphTypeName); if ($morphKey !== preg_replace("/_id\$/", '', $morphIdName)) { return $next($request); } $morphTypeName = $morphKey . '_type'; $morphIdName = $morphKey . '_id'; $morphType = $parameters[$morphTypeName]; $morphId = $parameters[$morphIdName]; // Not going to worry about custom keys here. $morphInstance = requireMorphInstance($morphType, $morphId); $route->forgetParameter($morphTypeName); $route->forgetParameter($morphIdName); $route->setParameter($morphKey, $morphInstance); return $next($request); }
/** * Find a model in the collection by key. * * @param mixed $key * @param mixed $default * @return \Illuminate\Database\Eloquent\Model */ public function find($key, $default = null) { if ($key instanceof Model) { $key = $key->getKey(); } return Arr::first($this->items, function ($model) use($key) { return $model->getKey() == $key; }, $default); }
/** * Parses the conditional parameter out of the action parameter. This is the parameter * given to WordPress conditional functions later. * * @param array $action The action parameter where the conditional parameters are in * * @return array An array with the conditional parameters or null */ protected function parseConditionalParameters($action) { // Retrieve parameters. Accept only string or array. // This help filter the $action parameters as it might also be a Closure. $parameters = Arr::first($action, function ($value, $key) { return is_string($value) || is_array($value); }); if ($this->condition() && !is_null($parameters)) { if (is_string($parameters) && strrpos($parameters, '@') !== false) { /** * In case of a controller value statement, return empty array. */ return []; } return is_array($parameters) ? $parameters : [$parameters]; } return []; }
/** * Get the relationship name of the image accessor for which images are enriched * * @return string */ protected function getRelationForImagesWithResizesCaller() { $self = __FUNCTION__; $caller = Arr::first(debug_backtrace(false), function ($key, $trace) use($self) { $caller = $trace['function']; return !in_array($caller, ['getImagesWithResizes']) && $caller != $self; }); if (is_null($caller)) { return null; } // strip 'get' from front and 'attribute' from rear return Str::camel(substr($caller['function'], 3, -9)); }
/** * Determine if an object of the given class is in a list of parameters. * * @param string $class * @param array $parameters * @return bool */ protected function alreadyInParameters($class, array $parameters) { return !is_null(Arr::first($parameters, function ($key, $value) use($class) { return $value instanceof $class; })); }
/** * Find a skill by its name * * @param $name * @return \Burthorpe\Runescape\RS3\Skills\Contract|null */ public function findByName($name) { return Arr::first($this->items, function ($key, Contract $skill) use($name) { return $skill->getName() === strtolower($name); }, null); }
private function getRouteMethod(Route $route) { $methods = $route->methods(); return Arr::first($methods); }
/** * Get the registered service provider instance if it exists. * * @param \Illuminate\Support\ServiceProvider|string $provider * @return \Illuminate\Support\ServiceProvider|null */ public function getProvider($provider) { $name = is_string($provider) ? $provider : get_class($provider); return Arr::first($this->serviceProviders, function ($value) use($name) { return $value instanceof $name; }); }
/** * Get the first item from the collection. * * @param callable|null $callback * @param mixed $default * @return mixed */ public function first(callable $callback = null, $default = null) { if (is_null($callback)) { return count($this->items) > 0 ? reset($this->items) : value($default); } return Arr::first($this->items, $callback, $default); }
/** * Get the extension used by the view file. * * @param string $path * @return string */ protected function getExtension($path) { $extensions = array_keys($this->extensions); return Arr::first($extensions, function ($key, $value) use($path) { return Str::endsWith($path, $value); }); }
/** * Get the environment argument from the console. * * @param array $args * @return string|null */ protected function getEnvironmentArgument(array $args) { return Arr::first($args, function ($k, $v) { return starts_with($v, '--env'); }); }
protected function buildMirrors($disks) { $main = $this->disk(Arr::first($disks))->getAdapter(); if (count($disks) > 2) { $second = $this->buildMirrors(array_slice($disks, 1)); } else { $second = $this->disk(Arr::last($disks))->getAdapter(); } return new \League\Flysystem\Replicate\ReplicateAdapter(new \Litipk\Flysystem\Fallback\FallbackAdapter($main, $second, true), $second); }
/** * Get a global scope registered with the model. * * @param ScopeInterface $scope * @return ScopeInterface|null */ public static function getGlobalScope($scope) { return Arr::first(static::$globalScopes[get_called_class()], function ($key, $value) use($scope) { return $scope instanceof $value; }); }
/** * Set the active context(s). * * @param mixed $contexts * @param bool $merge */ public function setContext($contexts, $merge = false) { $set = []; if (is_object($contexts)) { $contexts = [$contexts]; } foreach ((array) $contexts as $context) { if ($context instanceof AuthContext) { $set[get_class($context)] = $context; } elseif (is_string($context)) { if (!class_exists($context)) { $context = Arr::first($this->getRegisteredContexts(), function ($abstract, $concrete) use($context) { return $abstract == $context || $concrete == $context; }); } $set[$context] = $this->loadContext($context); } } $this->activeContexts = $merge ? array_merge($this->activeContexts, $set) : $set; }
/** * Determine if a route in the array matches the request. * * @param array $routes * @param \Illuminate\http\Request $request * @param bool $includingMethod * @return \Illuminate\Routing\Route|null */ protected function check(array $routes, $request, $includingMethod = true) { return Arr::first($routes, function ($key, $value) use($request, $includingMethod) { return $value->matches($request, $includingMethod); }); }
/** * Get the method name that called the internal methods for executing a call * * @return string|null */ protected function getDoCallCallerMethodName() { $self = __FUNCTION__; $caller = Arr::first(debug_backtrace(false), function ($key, $trace) use($self) { $caller = $trace['function']; return !in_array($caller, ['doCall', 'checkAccess', 'delete', 'get', 'patch', 'post', 'put']) && $caller != $self; }); if (is_null($caller)) { return null; } return $caller['function']; }
/** * Get the first item from the collection. * * @param callable|null $callback * @param mixed $default * @return mixed */ public function first(callable $callback = null, $default = null) { return Arr::first($this->items, $callback, $default); }
/** * Get the relationship name of the belongs to many. * * @return string */ protected function getBelongsToManyCaller() { $self = __FUNCTION__; $caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($key, $trace) use($self) { $caller = $trace['function']; return !in_array($caller, Model::$manyMethods) && $caller != $self; }); return !is_null($caller) ? $caller['function'] : null; }
/** * Get the environment argument from the console. * * @param array $args * * @return string|null */ protected function getEnvironmentArgument(array $args) { return Arr::first($args, function ($value) { return Str::startsWith($value, '--env'); }); }
/** * Find the callable in an action array. * * @param array $action * @return callable */ protected function findCallable(array $action) { return Arr::first($action, function ($key, $value) { return is_callable($value) && is_numeric($key); }); }
/** * Add a controller to the collection if it does not exist. If the * controller implements an interface suffixed with "Docs" it * will be used instead of the controller. * * @param \Illuminate\Support\Collection $controllers * @param object $controller * * @return void */ protected function addControllerIfNotExists(Collection $controllers, $controller) { $class = get_class($controller); if ($controllers->has($class)) { return; } $reflection = new ReflectionClass($controller); $interface = Arr::first($reflection->getInterfaces(), function ($key, $value) { return ends_with($key, 'Docs'); }); if ($interface) { $controller = $interface; } $controllers->put($class, $controller); }
/** * Return the first element in an array passing a given truth test. * * @param array $array * @param callable $callback * @param mixed $default * @return mixed */ function array_first($array, callable $callback, $default = null) { return Arr::first($array, $callback, $default); }
/** * @return mixed */ public function getActivePane() { if (!is_null($this->activePane)) { return $this->activePane; } if ($this->activate) { $this->activePane = Arr::first($this->items, function ($key, $item) { return $item->getId() == $this->activate; }); } if (!$this->activePane) { $this->activePane = reset($this->items); } return $this->activePane; }