flight\net\Route::matchUrl PHP Method

matchUrl() public method

Checks if a URL matches the route pattern. Also parses named parameters in the URL.
public matchUrl ( string $url, boolean $case_sensitive = false ) : boolean
$url string Requested URL
$case_sensitive boolean Case sensitive matching
return boolean Match status
    public function matchUrl($url, $case_sensitive = false)
    {
        // Wildcard or exact match
        if ($this->pattern === '*' || $this->pattern === $url) {
            return true;
        }
        $ids = array();
        $last_char = substr($this->pattern, -1);
        // Get splat
        if ($last_char === '*') {
            $n = 0;
            $len = strlen($url);
            $count = substr_count($this->pattern, '/');
            for ($i = 0; $i < $len; $i++) {
                if ($url[$i] == '/') {
                    $n++;
                }
                if ($n == $count) {
                    break;
                }
            }
            $this->splat = (string) substr($url, $i + 1);
        }
        // Build the regex for matching
        $regex = str_replace(array(')', '/*'), array(')?', '(/?|/.*?)'), $this->pattern);
        $regex = preg_replace_callback('#@([\\w]+)(:([^/\\(\\)]*))?#', function ($matches) use(&$ids) {
            $ids[$matches[1]] = null;
            if (isset($matches[3])) {
                return '(?P<' . $matches[1] . '>' . $matches[3] . ')';
            }
            return '(?P<' . $matches[1] . '>[^/\\?]+)';
        }, $regex);
        // Fix trailing slash
        if ($last_char === '/') {
            $regex .= '?';
        } else {
            $regex .= '/?';
        }
        // Attempt to match route and named parameters
        if (preg_match('#^' . $regex . '(?:\\?.*)?$#' . ($case_sensitive ? '' : 'i'), $url, $matches)) {
            foreach ($ids as $k => $v) {
                $this->params[$k] = array_key_exists($k, $matches) ? urldecode($matches[$k]) : null;
            }
            $this->regex = $regex;
            return true;
        }
        return false;
    }