AdamStipak\RestRoute::match PHP Method

match() public method

Maps HTTP request to a Request object.
public match ( Nette\Http\IRequest $httpRequest ) : Nette\Application\Request | null
$httpRequest Nette\Http\IRequest
return Nette\Application\Request | null
    public function match(IRequest $httpRequest)
    {
        $url = $httpRequest->getUrl();
        $basePath = Strings::replace($url->getBasePath(), '/\\//', '\\/');
        $cleanPath = Strings::replace($url->getPath(), "/^{$basePath}/");
        $path = Strings::replace($this->getPath(), '/\\//', '\\/');
        $pathRexExp = empty($path) ? "/^.+\$/" : "/^{$path}\\/.*\$/";
        if (!Strings::match($cleanPath, $pathRexExp)) {
            return NULL;
        }
        $cleanPath = Strings::replace($cleanPath, '/^' . $path . '\\//');
        $params = [];
        $path = $cleanPath;
        $params['action'] = $this->detectAction($httpRequest);
        $frags = explode('/', $path);
        if ($this->useURLModuleVersioning) {
            $version = array_shift($frags);
            if (!Strings::match($version, $this->versionRegex)) {
                array_unshift($frags, $version);
                $version = NULL;
            }
        }
        // Resource ID.
        if (count($frags) % 2 === 0) {
            $params['id'] = array_pop($frags);
        } elseif ($params['action'] == 'read') {
            $params['action'] = 'readAll';
        }
        $presenterName = Inflector::studlyCase(array_pop($frags));
        // Allow to use URLs like domain.tld/presenter.format.
        $formats = join('|', array_keys($this->formats));
        if (Strings::match($presenterName, "/.+\\.({$formats})\$/")) {
            list($presenterName) = explode('.', $presenterName);
        }
        // Associations.
        $assoc = [];
        if (count($frags) > 0 && count($frags) % 2 === 0) {
            foreach ($frags as $k => $f) {
                if ($k % 2 !== 0) {
                    continue;
                }
                $assoc[$f] = $frags[$k + 1];
            }
        }
        $params['format'] = $this->detectFormat($httpRequest);
        $params['associations'] = $assoc;
        $params['data'] = $this->readInput();
        $params['query'] = $httpRequest->getQuery();
        if ($this->useURLModuleVersioning) {
            $suffix = $presenterName;
            $presenterName = empty($this->module) ? "" : $this->module . ':';
            $presenterName .= array_key_exists($version, $this->versionToModuleMapping) ? $this->versionToModuleMapping[$version] . ":" . $suffix : $this->versionToModuleMapping[NULL] . ":" . $suffix;
        } else {
            $presenterName = empty($this->module) ? $presenterName : $this->module . ':' . $presenterName;
        }
        return new Request($presenterName, $httpRequest->getMethod(), $params, [], $httpRequest->getFiles());
    }

Usage Example

 /**
  * @expectedException \Nette\InvalidStateException
  */
 public function testOverrideMethodWithInvalidMethod()
 {
     $method = 'invalid';
     $route = new RestRoute('Api');
     $url = new UrlScript();
     $url->setPath('/api/foo');
     $url->setQuery(['__method' => $method]);
     $request = new Request($url, NULL, NULL, NULL, NULL, NULL, 'POST');
     $appRequest = $route->match($request);
 }
All Usage Examples Of AdamStipak\RestRoute::match