/** * 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; }
/** * 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; }