22
33namespace PHPCR \Util ;
44
5+ use PHPCR \NamespaceException ;
56use 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 }
0 commit comments