From 45fd296b09145b2eab6ac9adb18943e56d805366 Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Mon, 27 Jul 2015 18:02:55 +0200 Subject: [PATCH] absolutizePath(): added second parameter $prefix = '/' [BC-BREAK] --- readme.md | 9 ++++++++- src/PathsHelper.php | 4 ++-- .../PathsHelper/PathsHelper.absolutizePath.phpt | 16 ++++++++-------- tests/PathsHelper/data-absolutize-path.ini | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index fb65774..0212acc 100644 --- a/readme.md +++ b/readme.md @@ -15,7 +15,14 @@ $helper = new Cz\PathsHelper; $helper->absolutizePath('path/to/my/../text/./file.txt'); ``` -Returns ```path/to/text/file.txt``` +Returns ```/path/to/text/file.txt``` + +You can use second parameters ```$prefix```: + +``` php +$helper->absolutizePath('path/to/my/../text/./file.txt', NULL); // returns path/to/text/file.txt +$helper->absolutizePath('path/to/my/../text/./file.txt', '/file/root/'); // returns /file/root/path/to/text/file.txt +``` ### Generating relative paths diff --git a/src/PathsHelper.php b/src/PathsHelper.php index 40cdc46..c162b91 100644 --- a/src/PathsHelper.php +++ b/src/PathsHelper.php @@ -105,7 +105,7 @@ public function isPathCurrent($currentPath, $mask) * @param string * @return string */ - public function absolutizePath($path) + public function absolutizePath($path, $prefix = '/') { $path = explode('/', $path); $buffer = array(); @@ -120,6 +120,6 @@ public function absolutizePath($path) } } - return implode('/', $buffer); + return $prefix . implode('/', $buffer); } } diff --git a/tests/PathsHelper/PathsHelper.absolutizePath.phpt b/tests/PathsHelper/PathsHelper.absolutizePath.phpt index be86449..dd773ef 100644 --- a/tests/PathsHelper/PathsHelper.absolutizePath.phpt +++ b/tests/PathsHelper/PathsHelper.absolutizePath.phpt @@ -6,15 +6,15 @@ require __DIR__ . '/../../src/PathsHelper.php'; $h = new Cz\PathsHelper; -Assert::same('', $h->absolutizePath('')); -Assert::same('', $h->absolutizePath('..')); -Assert::same('', $h->absolutizePath('.')); +Assert::same('/', $h->absolutizePath('')); +Assert::same('/', $h->absolutizePath('..')); +Assert::same('/', $h->absolutizePath('.')); -Assert::same('lorem/ipsum/dolor/sit', $h->absolutizePath('lorem/ipsum/dolor/sit')); +Assert::same('/lorem/ipsum/dolor/sit', $h->absolutizePath('lorem/ipsum/dolor/sit')); -Assert::same('lorem/ipsum/dolor/sit', $h->absolutizePath('lorem/././ipsum/dolor/sit')); -Assert::same('ipsum/dolor/sit', $h->absolutizePath('lorem/././dolor/../../ipsum/dolor/sit')); - -Assert::same('ipsum/dolor/sit', $h->absolutizePath('./../.././lorem/././dolor/../../ipsum/dolor/sit/../sit/amet/..')); +Assert::same('/lorem/ipsum/dolor/sit', $h->absolutizePath('lorem/././ipsum/dolor/sit')); +Assert::same('/ipsum/dolor/sit', $h->absolutizePath('lorem/././dolor/../../ipsum/dolor/sit')); +Assert::same('/ipsum/dolor/sit', $h->absolutizePath('./../.././lorem/././dolor/../../ipsum/dolor/sit/../sit/amet/..')); +Assert::same('ipsum/dolor/sit', $h->absolutizePath('lorem/././dolor/../../ipsum/dolor/sit', NULL)); diff --git a/tests/PathsHelper/data-absolutize-path.ini b/tests/PathsHelper/data-absolutize-path.ini index 59967b8..9caf036 100644 --- a/tests/PathsHelper/data-absolutize-path.ini +++ b/tests/PathsHelper/data-absolutize-path.ini @@ -1,3 +1,3 @@ [readme] path = path/to/my/../text/./file.txt -result = path/to/text/file.txt +result = /path/to/text/file.txt