/** * @param string $url * @param string $root * * @return $this */ public function determineActiveForUrl(string $url, string $root = '/') { if (!$this->hasUrl()) { return; } $itemUrl = Url::fromString($this->url); $matchUrl = Url::fromString($url); // If the hosts don't match, this url isn't active. if ($itemUrl->getHost() !== $matchUrl->getHost()) { return $this->setInactive(); } // If this url doesn't start with the root, it's inactive. if (!Str::startsWith($itemUrl->getPath(), $root)) { return $this->setInactive(); } // For the next comparisons we just need the paths, and we'll remove // the root first. $itemPath = Str::removeFromStart($root, $itemUrl->getPath()); $matchPath = Str::removeFromStart($root, $matchUrl->getPath()); // If this url starts with the url we're matching with, it's active. if (Str::startsWith($matchPath, $itemPath)) { return $this->setActive(); } return $this->setInactive(); }