@@ -68,19 +68,10 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
6868 || strlen ($ path ) > 1 && '/ ' === $ path [strlen ($ path ) - 1 ]
6969 || preg_match ('-//|/\./|/\.\./- ' , $ path )
7070 ) {
71- if ($ throw ) {
72- throw new RepositoryException ("Invalid path $ path " );
73- }
74-
75- return false ;
71+ return self ::error ("Invalid path $ path " , $ throw );
7672 }
7773 if ($ destination && '] ' === $ path [strlen ($ path ) - 1 ]) {
78- if ($ throw ) {
79- throw new RepositoryException ("Destination path may not end with index $ path " );
80- }
81-
82- return false ;
83-
74+ return self ::error ("Destination path may not end with index $ path " , $ throw );
8475 }
8576
8677 return true ;
@@ -97,7 +88,8 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
9788 * encode and decode characters that are not natively allowed by a storage
9889 * engine.
9990 *
100- * @param string $name The name to check
91+ * @param string $name The name to check
92+ * @param boolean $throw whether to throw an exception on validation errors.
10193 *
10294 * @return bool true if valid, false if not valid and $throw was false
10395 *
@@ -108,11 +100,11 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
108100 public static function assertValidLocalName ($ name , $ throw = true )
109101 {
110102 if ('. ' == $ name || '.. ' == $ name ) {
111- throw new RepositoryException ( ' Name may not be parent or self identifier: ' . $ name );
103+ return self :: error ( " Name may not be parent or self identifier: $ name" , $ throw );
112104 }
113105
114106 if (preg_match ('/ \\/|:| \\[| \\]| \\|| \\*/ ' , $ name )) {
115- throw new RepositoryException ( ' Name contains illegal characters: ' . $ name );
107+ return self :: error ( " Name contains illegal characters: $ name" , $ throw );
116108 }
117109
118110 return true ;
@@ -143,30 +135,18 @@ public static function assertValidLocalName($name, $throw = true)
143135 public static function normalizePath ($ path , $ destination = false , $ throw = true )
144136 {
145137 if (!is_string ($ path )) {
146- if ($ throw ) {
147- throw new RepositoryException ('Expected string but got ' . gettype ($ path ));
148- }
149-
150- return false ;
138+ return self ::error ('Expected string but got ' . gettype ($ path ), $ throw );
151139 }
152140 if (strlen ($ path ) === 0 ) {
153- if ($ throw ) {
154- throw new RepositoryException ('Path must not be of zero length ' );
155- }
156-
157- return false ;
141+ return self ::error ('Path must not be of zero length ' , $ throw );
158142 }
159143
160144 if ('/ ' === $ path ) {
161145 return '/ ' ;
162146 }
163147
164148 if ('/ ' !== $ path [0 ]) {
165- if ($ throw ) {
166- throw new RepositoryException ("Not an absolute path ' $ path' " );
167- }
168-
169- return false ;
149+ return self ::error ("Not an absolute path ' $ path' " , $ throw );
170150 }
171151
172152 $ finalParts = array ();
@@ -220,25 +200,13 @@ public static function normalizePath($path, $destination = false, $throw = true)
220200 public static function absolutizePath ($ path , $ context , $ destination = false , $ throw = true )
221201 {
222202 if (!is_string ($ path )) {
223- if ($ throw ) {
224- throw new RepositoryException ('Expected string path but got ' . gettype ($ path ));
225- }
226-
227- return false ;
203+ return self ::error ('Expected string path but got ' . gettype ($ path ), $ throw );
228204 }
229205 if (!is_string ($ context )) {
230- if ($ throw ) {
231- throw new RepositoryException ('Expected string context but got ' . gettype ($ context ));
232- }
233-
234- return false ;
206+ return self ::error ('Expected string context but got ' . gettype ($ context ), $ throw );
235207 }
236208 if (strlen ($ path ) === 0 ) {
237- if ($ throw ) {
238- throw new RepositoryException ('Path must not be of zero length ' );
239- }
240-
241- return false ;
209+ return self ::error ('Path must not be of zero length ' , $ throw );
242210 }
243211
244212 if ('/ ' !== $ path [0 ]) {
@@ -297,4 +265,24 @@ public static function getPathDepth($path)
297265 {
298266 return substr_count (rtrim ($ path , '/ ' ), '/ ' );
299267 }
268+
269+ /**
270+ * If $throw is true, throw a RepositoryException with $msg. Otherwise
271+ * return false.
272+ *
273+ * @param string $msg the exception message to use in case of throw being true
274+ * @param boolean $throw whether to throw the exception or return false
275+ *
276+ * @return boolean false
277+ *
278+ * @throws RepositoryException
279+ */
280+ private static function error ($ msg , $ throw )
281+ {
282+ if ($ throw ) {
283+ throw new RepositoryException ($ msg );
284+ }
285+
286+ return false ;
287+ }
300288}
0 commit comments