Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/PHPCR/Util/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/'), '/');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a path with a trailing slash is invalid anyways. but lets be defensive here.

i fail to understand why the count for the root node '/' returns 0, but the test seems to prove it works as expected

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because rtrim('/', '/') === ''

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d'oh

}
}
7 changes: 7 additions & 0 deletions tests/PHPCR/Tests/Util/PathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/'));
}
}