Resolve relative on relative

master
Anton Smirnov 11 months ago
parent e878ae389d
commit 1401455c62
  1. 9
      src/AbstractPath.php
  2. 20
      src/RelativePath.php
  3. 9
      tests/RelativePathTest.php

@ -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;
}

@ -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;
}
}

@ -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());
}
}

Loading…
Cancel
Save