Example #1
0
 /**
  * @param \Alchemy\Component\Http\Request|string $mixed
  * @return array|bool
  * @throws \Exception
  */
 public function match($mixed)
 {
     $this->prepare();
     $urlString = $mixed;
     $requestMethod = '';
     if ($urlString instanceof Request) {
         $urlString = $mixed->getPathInfo();
         $requestMethod = strtolower($mixed->getMethod());
         // HEAD and GET are equivalent as per RFC
         if ('head' === ($method = strtolower($mixed->getMethod()))) {
             $requestMethod = 'get';
         }
     }
     $this->urlString = urldecode($urlString);
     if (!preg_match('/^' . $this->realPattern . '$/', $this->urlString, $compiledMatches)) {
         return false;
     }
     // to verify _method requirement was defined $requestMethod ahould defined from Request object
     if (array_key_exists('_method', $this->requirements) && !empty($requestMethod)) {
         // filter method that by requirement
         if (!in_array($requestMethod, $this->requirements['_method'])) {
             return false;
         }
     }
     if (!(isset($this->vars[1]) && count($compiledMatches) >= count($this->vars[1]))) {
         throw new \Exception("Error while matching parameters, url string given: '{$urlString}'");
     }
     $varValues = array_slice($compiledMatches, 1);
     foreach ($this->vars[1] as $i => $varName) {
         $this->parameters[$varName] = $varValues[$i];
         unset($varValues[$i]);
     }
     foreach ($varValues as $varValue) {
         if (substr($varValue, 0, 1) != '?') {
             $this->parameters[] = $varValue;
         }
     }
     $this->parameters = array_merge($this->defaults, $this->parameters);
     if (!empty($this->mapping)) {
         $this->parameters = array("params" => $this->parameters, "mapped" => $this->map($this->parameters));
     }
     return $this->parameters;
 }