92 if (
$path[0] !==
'/' ) {
98 if ( strpos(
$path,
'$1' ) ===
false ) {
99 if ( substr(
$path, -1 ) !==
'/' ) {
108 if ( !isset(
$params[
'title'] ) && strpos(
$path,
'$1' ) !==
false ) {
118 foreach (
$params as $paramName => $paramData ) {
119 if ( is_string( $paramData ) ) {
120 if ( preg_match(
'/\$(\d+|key)/u', $paramData ) ) {
121 $paramArrKey =
'pattern';
125 $paramArrKey =
'value';
128 $paramArrKey => $paramData
135 foreach (
$options as $optionName => $optionData ) {
136 if ( preg_match(
'/^\$\d+$/u', $optionName ) ) {
137 if ( !is_array( $optionData ) ) {
150 $this->patterns[] = $pattern;
161 if ( is_array(
$path ) ) {
162 foreach (
$path as $key => $onePath ) {
188 foreach ( $this->patterns
as $key => $pattern ) {
189 $weights[$key] = $pattern->weight;
191 array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
198 protected static function makeWeight( $pattern ) {
199 # Start with a weight of 0
203 $path = explode(
'/', $pattern->path );
205 # For each level of the path
207 if ( preg_match(
'/^\$(\d+|key)$/u', $piece ) ) {
208 # For a piece that is only a $1 variable add 1 points of weight
210 } elseif ( preg_match(
'/\$(\d+|key)/u', $piece ) ) {
211 # For a piece that simply contains a $1 variable add 2 points of weight
214 # For a solid piece add a full 3 points of weight
219 foreach ( $pattern->options
as $key => $option ) {
220 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
221 # Add 0.5 for restrictions to values
222 # This way given two separate "/$2/$1" patterns the
223 # one with a limited set of $2 values will dominate
224 # the one that'll match more loosely
245 foreach ( $this->patterns
as $pattern ) {
266 $regexp = preg_quote( $pattern->path,
'#' );
270 $regexp = preg_replace(
'#\\\\\$(\d+)#u',
'(?P<par$1>.+?)',
$regexp );
280 foreach ( $pattern->options
as $key => $option ) {
281 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
282 $n = intval( substr( $key, 1 ) );
283 $value = rawurldecode( $m[
"par{$n}"] );
284 if ( !in_array(
$value, $option ) ) {
293 foreach ( $m
as $matchKey => $matchValue ) {
294 if ( preg_match(
'/^par\d+$/u', $matchKey ) ) {
295 $n = intval( substr( $matchKey, 3 ) );
296 $data[
'$' .
$n] = rawurldecode( $matchValue );
300 if ( isset( $pattern->key ) ) {
301 $data[
'$key'] = $pattern->key;
305 foreach ( $pattern->params
as $paramName => $paramData ) {
309 if ( preg_match(
'/^data:/u', $paramName ) ) {
311 $key = substr( $paramName, 5 );
317 if ( isset( $paramData[
'value'] ) ) {
319 $value = $paramData[
'value'];
320 } elseif ( isset( $paramData[
'pattern'] ) ) {
322 $value = $paramData[
'pattern'];
324 $replacer->params = $m;
325 if ( isset( $pattern->key ) ) {
326 $replacer->key = $pattern->key;
344 if ( isset( $pattern->options[
'callback'] ) ) {
345 call_user_func_array( $pattern->options[
'callback'],
array( &
$matches, $data ) );
370 $this->error =
false;
371 $value = preg_replace_callback(
'/\$(\d+|key)/u',
array( $this,
'callback' ),
$value );
372 if ( $this->error ) {
383 if ( $m[1] ==
"key" ) {
384 if ( is_null( $this->
key ) ) {
391 if ( !isset( $this->params[
"par$d"] ) ) {
395 return rawurldecode( $this->params[
"par$d"] );