Skip to content

Commit cecd974

Browse files
committed
Merge pull request #130 from phpcr/localnodename
Added method to get a nodes localname
2 parents 057f17a + 4b7003b commit cecd974

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/PHPCR/Util/PathHelper.php

+20
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@ public static function getNodeName($path)
262262
return substr($path, $strrpos + 1);
263263
}
264264

265+
/**
266+
* Return the localname of the node at the given path.
267+
* The local name is the node name minus the namespace
268+
*
269+
* @param string $path a valid absolute path
270+
*
271+
* @return string The localname
272+
*/
273+
public static function getLocalNodeName($path)
274+
{
275+
$nodeName = self::getNodeName($path);
276+
$localName = strstr($nodeName, ':');
277+
278+
if (false !== $localName) {
279+
return substr($localName, 1);
280+
}
281+
282+
return $nodeName;
283+
}
284+
265285
/**
266286
* Get the depth of the path, ignore trailing slashes, root starts counting at 0
267287
*

tests/PHPCR/Tests/Util/PathHelperTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ public function dataproviderGetNodeName()
268268
);
269269
}
270270

271+
/**
272+
* @dataProvider dataproviderGetLocalNodeName
273+
*/
274+
public function testGetLocalNodeName($path, $expected = null)
275+
{
276+
$this->assertEquals($expected, PathHelper::getLocalNodeName($path));
277+
}
278+
279+
public function dataproviderGetLocalNodeName()
280+
{
281+
return array(
282+
array('/parent/child', 'child'),
283+
array('/foo:child', 'child'),
284+
array('/parent/ns:child', 'child'),
285+
array('/ns:parent/child:foo', 'foo'),
286+
array('/', ''),
287+
);
288+
}
289+
271290
/**
272291
* @expectedException \PHPCR\RepositoryException
273292
* @expectedExceptionMessage must be an absolute path

0 commit comments

Comments
 (0)