Skip to content

Commit

Permalink
absolutizePath(): added second parameter $prefix = '/' [BC-BREAK]
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Jul 27, 2015
1 parent 02f26af commit 45fd296
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/PathsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -120,6 +120,6 @@ public function absolutizePath($path)
}
}

return implode('/', $buffer);
return $prefix . implode('/', $buffer);
}
}
16 changes: 8 additions & 8 deletions tests/PathsHelper/PathsHelper.absolutizePath.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
2 changes: 1 addition & 1 deletion tests/PathsHelper/data-absolutize-path.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[readme]
path = path/to/my/../text/./file.txt
result = path/to/text/file.txt
result = /path/to/text/file.txt

0 comments on commit 45fd296

Please sign in to comment.