diff --git a/src/PHPCR/Util/PathHelper.php b/src/PHPCR/Util/PathHelper.php index eb0a9e9..71473ef 100644 --- a/src/PHPCR/Util/PathHelper.php +++ b/src/PHPCR/Util/PathHelper.php @@ -254,4 +254,16 @@ public static function getNodeName($path) { return substr($path, strrpos($path, '/') + 1); } + + /** + * Get the depth of the path, ignore trailing slashes, root starts counting at 0 + * + * @param string $path a valid absolute path, like /content/jobs/data + * + * @return integer with the path depth + */ + public static function getPathDepth($path) + { + return substr_count(rtrim($path, '/'), '/'); + } } diff --git a/tests/PHPCR/Tests/Util/PathHelperTest.php b/tests/PHPCR/Tests/Util/PathHelperTest.php index 388c292..c1f4d8b 100644 --- a/tests/PHPCR/Tests/Util/PathHelperTest.php +++ b/tests/PHPCR/Tests/Util/PathHelperTest.php @@ -236,4 +236,11 @@ public function testGetNodeNameRoot() $this->assertEquals('', PathHelper::getNodeName('/')); } + public function testGetPathDepth() + { + $this->assertEquals(0, PathHelper::getPathDepth('/')); + $this->assertEquals(1, PathHelper::getPathDepth('/foo')); + $this->assertEquals(2, PathHelper::getPathDepth('/foo/bar')); + $this->assertEquals(2, PathHelper::getPathDepth('/foo/bar/')); + } }