Esempio n. 1
0
 /**
  * Matches a user submitted path with parts defined by a map. Assigns and
  * returns an array of variables on a successful match.
  *
  * @param EvHttp_Controller_Request_Http $request Request to get the host from
  * @return array|false An array of assigned values or a false on a mismatch
  */
 public function match($request)
 {
     // Check the scheme if required
     if ($this->_scheme !== null) {
         $scheme = $request->getScheme();
         if ($scheme !== $this->_scheme) {
             return false;
         }
     }
     // Get the host and remove unnecessary port information
     $host = $request->getHttpHost();
     if (preg_match('#:\\d+$#', $host, $result) === 1) {
         $host = substr($host, 0, -strlen($result[0]));
     }
     $hostStaticCount = 0;
     $values = array();
     $host = trim($host, '.');
     if ($host != '') {
         $host = explode('.', $host);
         foreach ($host as $pos => $hostPart) {
             // Host is longer than a route, it's not a match
             if (!array_key_exists($pos, $this->_parts)) {
                 return false;
             }
             $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
             $hostPart = urldecode($hostPart);
             // If it's a static part, match directly
             if ($name === null && $this->_parts[$pos] != $hostPart) {
                 return false;
             }
             // If it's a variable with requirement, match a regex. If not - everything matches
             if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $hostPart)) {
                 return false;
             }
             // If it's a variable store it's value for later
             if ($name !== null) {
                 $values[$name] = $hostPart;
             } else {
                 $hostStaticCount++;
             }
         }
     }
     // Check if all static mappings have been matched
     if ($this->_staticCount != $hostStaticCount) {
         return false;
     }
     $return = $values + $this->_defaults;
     // Check if all map variables have been initialized
     foreach ($this->_variables as $var) {
         if (!array_key_exists($var, $return)) {
             return false;
         }
     }
     $this->_values = $values;
     return $return;
 }