Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Webmozart\PathUtil; Chris@0: Chris@0: use InvalidArgumentException; Chris@0: use Webmozart\Assert\Assert; Chris@0: Chris@0: /** Chris@0: * Contains utility methods for handling URL strings. Chris@0: * Chris@0: * The methods in this class are able to deal with URLs. Chris@0: * Chris@0: * @since 2.3 Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: * @author Claudio Zizza Chris@0: */ Chris@0: final class Url Chris@0: { Chris@0: /** Chris@0: * Turns a URL into a relative path. Chris@0: * Chris@0: * The result is a canonical path. This class is using functionality of Path class. Chris@0: * Chris@0: * @see Path Chris@0: * Chris@0: * @param string $url A URL to make relative. Chris@0: * @param string $baseUrl A base URL. Chris@0: * Chris@0: * @return string Chris@0: * Chris@0: * @throws InvalidArgumentException If the URL and base URL does Chris@0: * not match. Chris@0: */ Chris@0: public static function makeRelative($url, $baseUrl) Chris@0: { Chris@0: Assert::string($url, 'The URL must be a string. Got: %s'); Chris@0: Assert::string($baseUrl, 'The base URL must be a string. Got: %s'); Chris@0: Assert::contains($baseUrl, '://', '%s is not an absolute Url.'); Chris@0: Chris@0: list($baseHost, $basePath) = self::split($baseUrl); Chris@0: Chris@0: if (false === strpos($url, '://')) { Chris@0: if (0 === strpos($url, '/')) { Chris@0: $host = $baseHost; Chris@0: } else { Chris@0: $host = ''; Chris@0: } Chris@0: $path = $url; Chris@0: } else { Chris@0: list($host, $path) = self::split($url); Chris@0: } Chris@0: Chris@0: if ('' !== $host && $host !== $baseHost) { Chris@0: throw new InvalidArgumentException(sprintf( Chris@0: 'The URL "%s" cannot be made relative to "%s" since their host names are different.', Chris@0: $host, Chris@0: $baseHost Chris@0: )); Chris@0: } Chris@0: Chris@0: return Path::makeRelative($path, $basePath); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Splits a URL into its host and the path. Chris@0: * Chris@0: * ```php Chris@0: * list ($root, $path) = Path::split("http://example.com/webmozart") Chris@0: * // => array("http://example.com", "/webmozart") Chris@0: * Chris@0: * list ($root, $path) = Path::split("http://example.com") Chris@0: * // => array("http://example.com", "") Chris@0: * ``` Chris@0: * Chris@0: * @param string $url The URL to split. Chris@0: * Chris@0: * @return string[] An array with the host and the path of the URL. Chris@0: * Chris@0: * @throws InvalidArgumentException If $url is not a URL. Chris@0: */ Chris@0: private static function split($url) Chris@0: { Chris@0: $pos = strpos($url, '://'); Chris@0: $scheme = substr($url, 0, $pos + 3); Chris@0: $url = substr($url, $pos + 3); Chris@0: Chris@0: if (false !== ($pos = strpos($url, '/'))) { Chris@0: $host = substr($url, 0, $pos); Chris@0: $url = substr($url, $pos); Chris@0: } else { Chris@0: // No path, only host Chris@0: $host = $url; Chris@0: $url = '/'; Chris@0: } Chris@0: Chris@0: // At this point, we have $scheme, $host and $path Chris@0: $root = $scheme.$host; Chris@0: Chris@0: return array($root, $url); Chris@0: } Chris@0: }