You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
php-path/src/PathFactory.php

47 lines
1.2 KiB

<?php
declare(strict_types=1);
namespace Arokettu\Path;
final class PathFactory
{
public static function parse(string $path, array $urlSchemes = [], array $streamSchemes = []): PathInterface
{
if ($path[0] === '/') {
return UnixPath::parse($path);
}
if (preg_match('@^[a-zA-Z]:[\\\\/]@', $path) || str_starts_with($path, '\\\\')) {
return new WindowsPath($path);
}
if (preg_match('@^([-.+a-zA-Z0-9]+)://@', $path, $matches)) {
return self::parseUrlLike($path, $matches[1], $urlSchemes, $streamSchemes);
}
return RelativePath::currentOS($path);
}
private static function parseUrlLike(
string $path,
string $scheme,
array $urlSchemes = [],
array $streamSchemes = []
): PathInterface {
if ($urlSchemes === [] && $streamSchemes === []) {
return UrlPath::parse($path);
}
if (\in_array($scheme, $urlSchemes)) {
return UrlPath::parse($path);
}
if (\in_array($scheme, $streamSchemes)) {
return StreamPath::parse($path);
}
throw new \InvalidArgumentException('Unknown scheme: ' . $scheme);
}
}