$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($route->getName())->startsWith('fo.')) ->filter(fn ($route) => str($route->getName())->exactly($routeName))->count(); } // * METHODS /** * Get the link url * * @return string */ public function getUrl(): string { return $this->url ?? route($this->routeName); } /** * 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() { 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) { // phpcs:enable return \json_encode($this->jsonSerialize(), $options); } /** * Convert the object into something JSON serializable. * * @return array */ #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->toArray(); } /** * @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()}'"); } }