Skip to content

Commit 4c502c7

Browse files
committed
adds path concatenation
1 parent 39a7c0f commit 4c502c7

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

src/Components/Path.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ function getParts () : array {
7474
return $this->parts;
7575
}
7676

77-
/**
78-
* @return Path
79-
*/
77+
function getFileExtension () : string {
78+
return $this->fileExtension;
79+
}
80+
8081
function withFileExtensionRemoved () : Path {
8182
$path = (string) $this;
8283
$pathInfo = pathinfo($path);
@@ -92,11 +93,18 @@ function withFileExtensionRemoved () : Path {
9293
return self::createFromString($pathWithoutFileExtension);
9394
}
9495

95-
/**
96-
* @return string
97-
*/
98-
function getFileExtension () {
99-
return $this->fileExtension;
96+
function concat (Path $path) : Path {
97+
$next = clone $this;
98+
$next->hasTail = $path->hasTail;
99+
$next->fileExtension = $path->fileExtension;
100+
$next->parts = array_merge($next->parts, $path->parts);
101+
102+
// The only time we inherit absoluteness is if this path was empty
103+
if (empty($this->parts)) {
104+
$next->absolute = $path->absolute;
105+
}
106+
107+
return $next;
100108
}
101109

102110
/**

tests/PathComponentTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,33 @@ function provideEncodablePathStrings () : \Generator {
135135
yield '/foo%2Fbar/baz/' => ['/foo%2Fbar/baz/', '/foo%2fbar/baz/'];
136136
}
137137

138+
/**
139+
* @param string $expectedPath
140+
* @param string $initialPath
141+
* @param string ...$concatPaths
142+
*
143+
* @dataProvider provideConcatPaths
144+
*/
145+
function testExpectsPathsToBeConcatenated (string $expectedPath, string $expectedFileExtension, bool $expectedIsAbsolute, bool $expectedHasTail, string $initialPath, string ...$concatPaths) {
146+
$path = Path::createFromString($initialPath);
147+
foreach ($concatPaths as $concatPath) {
148+
$path = $path->concat(Path::createFromString($concatPath));
149+
}
150+
151+
$this->assertSame($expectedPath, $path->compose());
152+
$this->assertSame($expectedFileExtension, $path->getFileExtension());
153+
$this->assertSame($expectedIsAbsolute, $path->isAbsolute());
154+
$this->assertSame($expectedHasTail, $path->hasTail());
155+
}
156+
157+
function provideConcatPaths () : \Generator {
158+
yield ['/foo/bar', '', true, false, '/foo', '/bar'];
159+
yield ['/foo/bar', '', true, false, '/foo', 'bar'];
160+
yield ['foo/bar', '', false, false, 'foo', 'bar'];
161+
yield ['/foo', '', true, false, '', '/foo'];
162+
yield ['/foo/bar/', '', true, true, '', '/foo', 'bar/'];
163+
yield ['/foo/bar', '', true, false, '/foo/', '/bar'];
164+
yield ['/foo.php/bar.html', 'html', true, false, '/foo.php', 'bar.html'];
165+
}
166+
138167
}

0 commit comments

Comments
 (0)