Skip to content

Commit

Permalink
feat(core): `\LastDragon_ru\LaraASP\Core\Path\DirectoryPath::isInside…
Browse files Browse the repository at this point in the history
…()` added.
  • Loading branch information
LastDragon-ru committed Dec 13, 2024
1 parent 86bd5fc commit ed142ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/Path/DirectoryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Override;

use function str_starts_with;

class DirectoryPath extends Path {
#[Override]
public function getParentPath(): self {
Expand All @@ -14,4 +16,12 @@ public function getParentPath(): self {
protected function getDirectory(): self {
return $this;
}

public function isInside(Path $path): bool {
$path = (string) $this->getPath($path);
$root = (string) $this;
$inside = $path !== $root && str_starts_with($path, "{$root}/");

return $inside;
}
}
10 changes: 10 additions & 0 deletions packages/core/src/Path/DirectoryPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public function testGetParentPath(): void {
self::assertEquals('/absolute/path/to', (string) $absolute->getParentPath());
self::assertEquals('/absolute/path', (string) $absolute->getParentPath()->getParentPath());
}

public function testIsInside(): void {
$path = new DirectoryPath('/path/to/directory');

self::assertFalse($path->isInside($path));
self::assertTrue($path->isInside(new FilePath('/path/to/directory/file.md')));
self::assertTrue($path->isInside(new FilePath('file.md')));
self::assertFalse($path->isInside(new FilePath('/path/to/directory/../file.md')));
self::assertFalse($path->isInside(new FilePath('/path/to/file.md')));
}
}

0 comments on commit ed142ce

Please sign in to comment.