$url], ['url' => 'url'])->fails()) { throw new \RuntimeException("Url '{$url} is not a valid url"); } } /** * Check has route * * @param string $routeName * @return boolean */ private function hasRouteName(string $routeName): bool { /** @var \Illuminate\Routing\Router $router */ $router = app('router'); return (bool)collect($router->getRoutes()->getRoutes()) ->filter(fn($route) => Str::of($route->getName())->startsWith('fo.')) ->filter(fn($route) => Str::of($route->getName())->exactly($routeName))->count(); } // * METHODS /** * Get the link url * * @return string */ public function getUrl(): string { return !is_null($this->url) ? $this->url : route($this->getRouteName()); } /** * Get the unformatted url. * * @return string|null */ public function getUnformattedUrl(): string|null { return $this->url; } /** * Get the route name if the link is bound to a route * * @return null|string */ public function getRouteName(): ?string { return $this->routeName; } // * TRANSFORMERS /** * Get the instance as an array. * * @return array<'url'|'routeName', string|null> */ public function toArray(): array { return ['url' => $this->url, 'routeName' => $this->routeName]; } /** * Convert the object to its JSON representation. * * @param integer $options * @return string * @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing */ public function toJson($options = 0): string { // phpcs:enable return \json_encode($this->toArray(), $options); } /** * @return string * @throws \RuntimeException If the link cannot be converted to string. */ public function __toString() { if (!is_null($this->url)) { return $this->url; } if (!is_null($this->routeName)) { return route($this->routeName); } throw new \RuntimeException("This link could not be converted to string '{$this->toJson()}'"); } }