Exemplo n.º 1
0
 /**
  * Compile path to regular expression
  *
  * @return $this
  */
 protected function compileRegExp()
 {
     $exp = str_replace(array('(', ')'), array('(', ')?'), $this->content);
     foreach ($this->route->getParams() as $name => $config) {
         $class = isset($config['regExp']) ? $config['regExp'] : self::PARAM;
         $exp = str_replace('{' . $name . '}', '(' . $class . ')', $exp);
     }
     $exp = ':^' . $exp . '$:u';
     $this->regExp = $exp;
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Process route parameters
  *
  * @param Route $route  Route
  * @param array $params Passed parameters
  *
  * @return array
  * @throws \InvalidArgumentException
  */
 protected function processParams(Route $route, array $params)
 {
     $config = $route->getParams();
     foreach ($config as $name => $data) {
         // Apply default value if not specified (optional segment)
         if (!array_key_exists($name, $params) && array_key_exists('default', $data)) {
             $params[$name] = $data['default'];
         }
         // Reverse map parameter names
         if (array_key_exists($name, $params) && isset($data['map'])) {
             $map = $data['map'];
             $value = $params[$name];
             if (!is_array($map)) {
                 throw new \InvalidArgumentException('Route parameter map should be an array.');
             }
             $param = array_search($value, $map);
             if ($param !== false) {
                 $params[$name] = $param;
             }
         }
     }
     return $params;
 }