You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
954 B
PHP
38 lines
954 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Arokettu\Path;
|
|
|
|
final class UnixPath extends FilesystemPath
|
|
{
|
|
public static function parse(string $path, bool $strict = false): self
|
|
{
|
|
return new self($path, $strict);
|
|
}
|
|
|
|
protected function parsePath(string $path, bool $strict): void
|
|
{
|
|
if ($path[0] !== '/') {
|
|
throw new \InvalidArgumentException('Valid unix path must begin with a slash');
|
|
}
|
|
|
|
$components = explode('/', $path);
|
|
|
|
$parsedComponents = $this->normalize($components);
|
|
|
|
if ($parsedComponents->count() > 0 && $parsedComponents[0] === '..') {
|
|
if ($strict) {
|
|
throw new \InvalidArgumentException('Path went beyond root');
|
|
}
|
|
|
|
do {
|
|
$parsedComponents->shift();
|
|
} while ($parsedComponents[0] === '..');
|
|
}
|
|
|
|
$this->prefix = '/';
|
|
$this->components = $parsedComponents;
|
|
}
|
|
}
|