本文整理汇总了PHP中October\Rain\Router\Helper::getSegmentRegExp方法的典型用法代码示例。如果您正苦于以下问题:PHP Helper::getSegmentRegExp方法的具体用法?PHP Helper::getSegmentRegExp怎么用?PHP Helper::getSegmentRegExp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类October\Rain\Router\Helper
的用法示例。
在下文中一共展示了Helper::getSegmentRegExp方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSegmentRegexp
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);
}
示例2: resolveUrl
/**
* 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;
}