public function testSegmentRegexp() { $value = Helper::getSegmentRegExp(':my_param_name'); $this->assertFalse($value); $value = Helper::getSegmentRegExp(':my_param_name?'); $this->assertFalse($value); $value = Helper::getSegmentRegExp(':my_param_name?default value'); $this->assertFalse($value); $value = Helper::getSegmentRegExp(':my_param_name|^[a-z]+[0-9]?$|^[a-z]{3}$'); $this->assertEquals('/^[a-z]+[0-9]?$|^[a-z]{3}$/', $value); $value = Helper::getSegmentRegExp(':my_param_name?default value|^[a-z]+[0-9]?$'); $this->assertEquals('/^[a-z]+[0-9]?$/', $value); }
/** * Checks whether a given URL matches a given pattern. * @param string $url The URL to check. * @param array $parameters A reference to a PHP array variable to return the parameter list fetched from URL. * @return boolean Returns true if the URL matches the pattern. Otherwise returns false. */ public function resolveUrl($url, &$parameters) { $parameters = []; $patternSegments = $this->segments; $patternSegmentNum = count($patternSegments); $urlSegments = Helper::segmentizeUrl($url); /* * Only one wildcard can be used, if found, pull out the excess segments */ if ($this->wildSegmentCount === 1) { $wildSegments = $this->captureWildcardSegments($urlSegments); } /* * If the number of URL segments is more than the number of pattern segments - return false */ if (count($urlSegments) > count($patternSegments)) { return false; } /* * Compare pattern and URL segments */ foreach ($patternSegments as $index => $patternSegment) { $patternSegmentLower = mb_strtolower($patternSegment); if (strpos($patternSegment, ':') !== 0) { /* * Static segment */ if (!array_key_exists($index, $urlSegments) || $patternSegmentLower != mb_strtolower($urlSegments[$index])) { return false; } } else { /* * Dynamic segment. Initialize the parameter */ $paramName = Helper::getParameterName($patternSegment); $parameters[$paramName] = false; /* * Determine whether it is optional */ $optional = Helper::segmentIsOptional($patternSegment); /* * Check if the optional segment has no required segments following it */ if ($optional && $index < $patternSegmentNum - 1) { for ($i = $index + 1; $i < $patternSegmentNum; $i++) { if (!Helper::segmentIsOptional($patternSegments[$i])) { $optional = false; break; } } } /* * If the segment is optional and there is no corresponding value in the URL, assign the default value (if provided) * and skip to the next segment. */ $urlSegmentExists = array_key_exists($index, $urlSegments); if ($optional && !$urlSegmentExists) { $parameters[$paramName] = Helper::getSegmentDefaultValue($patternSegment); continue; } /* * If the segment is not optional and there is no corresponding value in the URL, return false */ if (!$optional && !$urlSegmentExists) { return false; } /* * Validate the value with the regular expression */ $regexp = Helper::getSegmentRegExp($patternSegment); if ($regexp) { try { if (!preg_match($regexp, $urlSegments[$index])) { return false; } } catch (\Exception $ex) { } } /* * Set the parameter value */ $parameters[$paramName] = $urlSegments[$index]; /* * Determine if wildcard and add stored paramters as a suffix */ if (Helper::segmentIsWildcard($patternSegment) && count($wildSegments)) { $parameters[$paramName] .= Helper::rebuildUrl($wildSegments); } } } return true; }