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