Skip to content

Commit dfd74b4

Browse files
committed
add namespace support to assertValidAbsolutePath
1 parent 5398f5c commit dfd74b4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/PHPCR/Util/PathHelper.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPCR\Util;
44

5+
use PHPCR\NamespaceException;
56
use PHPCR\RepositoryException;
67

78
/**
@@ -36,12 +37,14 @@ private function __construct()
3637
* move), meaning [] is not allowed. If your implementation does not
3738
* support same name siblings, just always pass true for this
3839
* @param bool $throw whether to throw an exception on validation errors
40+
* @param array|bool $namespacePrefixes List of all known namespace prefixes.
41+
* If specified, this method validates that the path contains no unknown prefixes.
3942
*
4043
* @return bool true if valid, false if not valid and $throw was false
4144
*
4245
* @throws RepositoryException if the path contains invalid characters and $throw is true
4346
*/
44-
public static function assertValidAbsolutePath($path, $destination = false, $throw = true)
47+
public static function assertValidAbsolutePath($path, $destination = false, $throw = true, $namespacePrefixes = false)
4548
{
4649
if ((!is_string($path) && !is_numeric($path))
4750
|| strlen($path) == 0
@@ -54,6 +57,17 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
5457
if ($destination && ']' === $path[strlen($path) - 1]) {
5558
return self::error("Destination path may not end with index: '$path'", $throw);
5659
}
60+
if ($namespacePrefixes) {
61+
$matches = array();
62+
preg_match_all('#/(?P<prefixes>[^:]+):#', $path, $matches);
63+
$unknown = array_diff(array_unique($matches['prefixes']), $namespacePrefixes);
64+
if (count($unknown)) {
65+
if (!$throw) {
66+
return false;
67+
}
68+
throw new NamespaceException(sprintf('Unknown namespace prefix(es) (%s) in path %s', implode(' and ', $unknown), $path));
69+
}
70+
}
5771

5872
return true;
5973
}

tests/PHPCR/Tests/Util/PathHelperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public function testAssertInvalidAbsolutePath($path, $destination = false)
3737
PathHelper::assertValidAbsolutePath($path, $destination);
3838
}
3939

40+
/**
41+
* @expectedException \PHPCR\NamespaceException
42+
* @expectedExceptionMessage invalidprefix and other-ns
43+
*/
44+
public function testAssertInvalidNamespaceAbsolutePath()
45+
{
46+
PathHelper::assertValidAbsolutePath('/invalidprefix:localname/other-ns:test/invalidprefix:node/bla', false, true, array('jcr', 'nt'));
47+
}
48+
4049
/**
4150
* @dataProvider dataproviderInvalidAbsolutePaths
4251
*/

0 commit comments

Comments
 (0)