Skip to content

Commit 6eb5878

Browse files
committed
Added Path::normalize()
1 parent b665d08 commit 6eb5878

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
* 2.2.0 (2015-08-14)
5+
6+
* added `Path::normalize()`
7+
48
* 2.1.0 (2015-07-14)
59

610
* `Path::canonicalize()` now turns `~` into the user's home directory on

src/Path.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ public static function canonicalize($path)
139139
return $canonicalPath;
140140
}
141141

142+
/**
143+
* Normalizes the given path.
144+
*
145+
* During normalization, all slashes are replaced by forward slashes ("/").
146+
* Contrary to {@link canonicalize()}, this method does not remove invalid
147+
* or dot path segments. Consequently, it is much more efficient and should
148+
* be used whenever the given path is known to be a valid, absolute system
149+
* path.
150+
*
151+
* This method is able to deal with both UNIX and Windows paths.
152+
*
153+
* @param string $path A path string.
154+
*
155+
* @return string The normalized path.
156+
*
157+
* @since 2.2 Added method.
158+
*/
159+
public static function normalize($path)
160+
{
161+
Assert::string($path, 'The path must be a string. Got: %s');
162+
163+
return str_replace('\\', '/', $path);
164+
}
165+
142166
/**
143167
* Returns the directory part of the path.
144168
*

tests/PathTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,4 +1287,17 @@ public function testGetHomeDirectoryForWindows()
12871287

12881288
$this->assertEquals('C:/users/webmozart', Path::getHomeDirectory());
12891289
}
1290+
1291+
public function testNormalize()
1292+
{
1293+
$this->assertSame('C:/Foo/Bar/test', Path::normalize('C:\\Foo\\Bar/test'));
1294+
}
1295+
1296+
/**
1297+
* @expectedException \InvalidArgumentException
1298+
*/
1299+
public function testNormalizeFailsIfNoString()
1300+
{
1301+
Path::normalize(true);
1302+
}
12901303
}

0 commit comments

Comments
 (0)