Skip to content

Commit a21f770

Browse files
committed
Merge pull request #52 from phpcr/path_depth
added PathHelper::getPathDepth()
2 parents 602c504 + bf2bd93 commit a21f770

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/PHPCR/Util/PathHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,16 @@ public static function getNodeName($path)
254254
{
255255
return substr($path, strrpos($path, '/') + 1);
256256
}
257+
258+
/**
259+
* Get the depth of the path, ignore trailing slashes, root starts counting at 0
260+
*
261+
* @param string $path a valid absolute path, like /content/jobs/data
262+
*
263+
* @return integer with the path depth
264+
*/
265+
public static function getPathDepth($path)
266+
{
267+
return substr_count(rtrim($path, '/'), '/');
268+
}
257269
}

tests/PHPCR/Tests/Util/PathHelperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,11 @@ public function testGetNodeNameRoot()
236236
$this->assertEquals('', PathHelper::getNodeName('/'));
237237
}
238238

239+
public function testGetPathDepth()
240+
{
241+
$this->assertEquals(0, PathHelper::getPathDepth('/'));
242+
$this->assertEquals(1, PathHelper::getPathDepth('/foo'));
243+
$this->assertEquals(2, PathHelper::getPathDepth('/foo/bar'));
244+
$this->assertEquals(2, PathHelper::getPathDepth('/foo/bar/'));
245+
}
239246
}

0 commit comments

Comments
 (0)