directorySeparator = $directorySeparator; } /** * Return the URL for a given path. * * @param string $path * * @return string */ public function __invoke(string $path = '/'): string { return $this->escape($this->stripLeadingSlashes($path)); } /** * Strip all leading slashes (and a single dot) from a path. * * @param string $path * * @return string */ protected function stripLeadingSlashes(string $path): string { return preg_replace('/^\.?(\/|\\\)+/', '', $path); } /** * Escape URL characters in path segments. * * @param string $path * * @return string */ protected function escape(string $path): string { return Str::explode($path, $this->directorySeparator)->map( static function (string $segment): string { return rawurlencode($segment); } )->implode($this->directorySeparator); } }