Resolve relative on relative

master
Anton Smirnov 2021-10-31 06:13:35 +02:00
parent e878ae389d
commit 1401455c62
3 changed files with 29 additions and 9 deletions

View File

@ -37,7 +37,7 @@ abstract class AbstractPath implements PathInterface
/**
* @return static
*/
private function doResolveRelative(RelativePath $path, bool $strict): self
protected function doResolveRelative(RelativePath $path, bool $strict): self
{
$relativeComponents = $path->components;
@ -56,7 +56,12 @@ abstract class AbstractPath implements PathInterface
continue;
}
if ($relativeComponents[$i] === '..') {
if (
$relativeComponents[$i] === '..' &&
!$components->isEmpty() &&
$components->top() !== '..' &&
$components->top() !== '.'
) {
$components->pop();
continue;
}

View File

@ -76,4 +76,24 @@ final class RelativePath extends AbstractPath
return $path;
}
protected function normalizeHead(\SplDoublyLinkedList $components, bool $strict): \SplDoublyLinkedList
{
if ($this->isRoot()) {
return parent::normalizeHead($components, $strict);
}
while (!$components->isEmpty()) {
if ($components[0] === '.') {
$components->shift();
continue;
}
break;
}
$components->unshift('.');
return $components;
}
}

View File

@ -54,7 +54,7 @@ class RelativePathTest extends TestCase
'/i/am/test/path',
'/i/am/test/path/i/am/test/path',
'/i/am/i/am/test/path',
null
'/i/am/test/path',
],
[
'/i/am/test/path',
@ -66,7 +66,7 @@ class RelativePathTest extends TestCase
'/i/am/test/path',
'../../i/am/test/path/i/am/test/path',
'../../i/am/i/am/test/path',
'../../../../../../i/am/test/path'
'../../../../../../i/am/test/path',
],
[
'/i/am/test/path',
@ -80,11 +80,6 @@ class RelativePathTest extends TestCase
foreach ($relativePaths as $rpi => $rp) {
$matrixResult = $matrix[$pi][$rpi];
if ($matrixResult === null) {
// TODO: should throw
continue;
}
self::assertEquals($matrixResult, $p->resolveRelative($rp)->toString());
}
}