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.
php-path/src/AbstractPath.php

145 lines
3.7 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace Arokettu\Path;
use Arokettu\Path\Helpers\DataTypeHelper;
/**
* @internal
*/
2 years ago
abstract class AbstractPath implements PathInterface
{
protected string $prefix;
protected \SplDoublyLinkedList $components;
2 years ago
abstract protected function parsePath(string $path, bool $strict): void;
2 years ago
2 years ago
public function __construct(string $path, bool $strict = false)
2 years ago
{
2 years ago
$this->parsePath($path, $strict);
2 years ago
}
/**
* @return static
2 years ago
*/
public function resolveRelative(RelativePathInterface $path, bool $strict = false): self
2 years ago
{
if ($path instanceof RelativePath) {
// optimize
$relativeComponents = $path->components;
} else {
// allow external implementations
$relativeComponents = $path->getComponents();
if (!array_is_list($relativeComponents)) {
throw new \InvalidArgumentException(
'Poor RelativePathInterface implementation: getComponents() must return a list'
);
}
}
2 years ago
if ($path->isRoot()) {
$newPath = clone $this;
$newPath->components = DataTypeHelper::iterableToNewListInstance($relativeComponents);
2 years ago
return $newPath;
}
$components = clone $this->components;
$numComponents = \count($relativeComponents);
2 years ago
for ($i = 0; $i < $numComponents; $i++) {
if ($relativeComponents[$i] === '.') {
continue;
}
if (
$relativeComponents[$i] === '..' &&
!$components->isEmpty() &&
$components->top() !== '..' &&
$components->top() !== '.'
) {
2 years ago
$components->pop();
continue;
}
$components->push($relativeComponents[$i]);
}
$newPath = clone $this;
$newPath->components = $this->normalizeHead($components, $strict);
return $newPath;
}
protected function normalize(array $components): \SplDoublyLinkedList
{
$componentsList = new \SplDoublyLinkedList();
$prevComponent = null;
foreach ($components as $component) {
if ($component === '.' || $component === '') {
continue;
}
2 years ago
if (
$component === '..' &&
$prevComponent !== '..' && $prevComponent !== null && // leading ..'s
$componentsList->count() > 0 // beginning of the list
) {
2 years ago
$componentsList->pop();
2 years ago
continue;
2 years ago
}
2 years ago
$componentsList->push($component);
2 years ago
$prevComponent = $component;
}
return $componentsList;
}
protected function normalizeHead(\SplDoublyLinkedList $components, bool $strict): \SplDoublyLinkedList
{
while (!$components->isEmpty()) {
if ($components[0] === '.') {
$components->shift();
continue;
}
if ($components[0] === '..') {
if ($strict) {
throw new \InvalidArgumentException('Relative path went beyond root');
}
$components->shift();
continue;
}
break;
}
return $components;
}
public function toString(): string
{
return $this->prefix . \iter\join('/', $this->components);
}
2 years ago
public function __toString(): string
{
return $this->toString();
}
2 years ago
public function getPrefix(): string
{
return $this->prefix;
}
2 years ago
public function getComponents(): array
{
return iterator_to_array($this->components, false);
}
}