Skip to content

Commit 0166fa3

Browse files
committed
Update Issue 39
Added splitPath method. This will replace basename and dirname
1 parent b43e5a3 commit 0166fa3

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/Sabre/DAV/URLUtil.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,33 @@ static function decodePathSegment($path) {
112112

113113
}
114114

115+
/**
116+
* Returns the 'dirname' and 'basename' for a path.
117+
*
118+
* The reason there is a custom function for this purpose, is because
119+
* basename() is locale aware (behaviour changes if C locale or a UTF-8 locale is used)
120+
* and we need a method that just operates on UTF-8 characters.
121+
*
122+
* In addition basename and dirname are platform aware, and will treat backslash (\) as a
123+
* directory separator on windows.
124+
*
125+
* This method returns the 2 components as an array.
126+
*
127+
* If there is no dirname, it will return an empty string. Any / appearing at the end of the
128+
* string is stripped off.
129+
*
130+
* @param string $path
131+
* @return array
132+
*/
133+
static function splitPath($path) {
134+
135+
$matches = array();
136+
if(preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u',$path,$matches)) {
137+
return array($matches[1],$matches[2]);
138+
} else {
139+
return array(null,null);
140+
}
141+
142+
}
115143

116144
}

tests/Sabre/DAV/URLUtilTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,38 @@ function testDecodeAccentsWindows7() {
9191

9292
}
9393

94+
function testSplitPath() {
95+
96+
$strings = array(
97+
98+
// input // expected result
99+
'/foo/bar' => array('/foo','bar'),
100+
'/foo/bar/' => array('/foo','bar'),
101+
'foo/bar/' => array('foo','bar'),
102+
'foo/bar' => array('foo','bar'),
103+
'foo/bar/baz' => array('foo/bar','baz'),
104+
'foo/bar/baz/' => array('foo/bar','baz'),
105+
'foo' => array('','foo'),
106+
'foo/' => array('','foo'),
107+
'/foo/' => array('','foo'),
108+
'/foo' => array('','foo'),
109+
110+
// UTF-8
111+
"/\xC3\xA0fo\xC3\xB3/bar" => array("/\xC3\xA0fo\xC3\xB3",'bar'),
112+
"/\xC3\xA0foo/b\xC3\xBCr/" => array("/\xC3\xA0foo","b\xC3\xBCr"),
113+
"foo/\xC3\xA0\xC3\xBCr" => array("foo","\xC3\xA0\xC3\xBCr"),
114+
115+
);
116+
117+
foreach($strings as $input => $expected) {
118+
119+
$output = Sabre_DAV_URLUtil::splitPath($input);
120+
$this->assertEquals($expected, $output, 'The expected output for \'' . $input . '\' was incorrect');
121+
122+
123+
}
124+
125+
126+
}
127+
94128
}

0 commit comments

Comments
 (0)