From 0612edffb868f5dd8ca22be39cd3b04e50fe6536 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 17 May 2024 09:57:00 +0400 Subject: [PATCH] feat(core): `Path::isNormalized()`. (cherry picked from commit d6ded7fb02d2516d1cdffd076238b7d976ccfd2c) --- packages/core/src/Utils/Path.php | 4 ++++ packages/core/src/Utils/PathTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/core/src/Utils/Path.php b/packages/core/src/Utils/Path.php index 3e5ce7c1..63e76c6d 100644 --- a/packages/core/src/Utils/Path.php +++ b/packages/core/src/Utils/Path.php @@ -39,6 +39,10 @@ public static function isAbsolute(string $path): bool { return SymfonyPath::isAbsolute(static::normalize($path)); } + public static function isNormalized(string $path): bool { + return static::normalize($path) === $path; + } + public static function normalize(string $path): string { return SymfonyPath::canonicalize($path); } diff --git a/packages/core/src/Utils/PathTest.php b/packages/core/src/Utils/PathTest.php index f511ec0c..b8264200 100644 --- a/packages/core/src/Utils/PathTest.php +++ b/packages/core/src/Utils/PathTest.php @@ -26,4 +26,29 @@ public function testGetRelativePath(): void { self::assertEquals('../to/file', Path::getRelativePath('/any/path', '/any/path/../to/file')); self::assertEquals('to/file', Path::getRelativePath('/absolute/path', 'to/./file')); } + + public function testJoin(): void { + self::assertEquals('/any/path', Path::join('/any/path')); + self::assertEquals('/any/path', Path::join('/any', 'path')); + self::assertEquals('/path', Path::join('/any', '..', 'path')); + self::assertEquals('any/path', Path::join('.', 'any', '.', 'path')); + self::assertEquals('../any/path', Path::join('..', 'any', '.', 'path')); + } + + public function testNormalize(): void { + self::assertEquals('/any/path', Path::normalize('/any/path')); + self::assertEquals('any/path', Path::normalize('any/path')); + self::assertEquals('any/path', Path::normalize('./any/path')); + self::assertEquals('any/path', Path::normalize('././any/path')); + self::assertEquals('../any/path', Path::normalize('./../any/path')); + self::assertEquals('path', Path::normalize('./any/../path')); + } + + public function testIsNormalized(): void { + self::assertTrue(Path::isNormalized('/any/path')); + self::assertTrue(Path::isNormalized('any/path')); + self::assertFalse(Path::isNormalized('./any/path')); + self::assertFalse(Path::isNormalized('././any/path')); + self::assertFalse(Path::isNormalized('./../any/path')); + } }