123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace Cube\Controller\Router\Route;
- class Rewrite extends AbstractRoute
- {
-
- private $_vars;
-
- protected $_assembled = array();
-
- public function assemble($params, $named = false)
- {
- $k = json_encode($params, $named);
- if (array_key_exists($k, $this->_assembled)) {
- return $this->_assembled[$k];
- }
- if (!is_array($params)) {
- $params = (array)$params;
- }
- $routeParams = $this->getParams();
- if ($named === false) {
- $routeParams['module'] = $this->normalize(
- $this->getModule(), true);
- foreach ($this->_defaults as $key => $value) {
- $routeParams[(string)$key] = (string)$value;
- }
- }
- $this->_vars = $params;
- if (array_key_exists('module', $params)) {
- if (strcmp($routeParams['module'], $params['module']) === 0) {
- unset($routeParams['module']);
- unset($params['module']);
- }
- }
- foreach ($params as $key => $value) {
- if (isset($this->_defaults[$key])) {
- if ($this->_defaults[$key] == $value) {
- unset($params[$key]);
- unset($routeParams[$key]);
- }
- }
- if (isset($this->_conditions[$key]) && is_string($value)) {
- if (preg_match('#(' . $this->_conditions[$key] . ')#', $value)) {
- unset($params[$key]);
- unset($routeParams[$key]);
- }
- }
- if (array_key_exists($key, $this->_params)) {
- unset($routeParams[$key]);
- }
- }
- if (empty($routeParams)) {
- $url = array();
- $get = array();
- foreach ((array)$params as $key => $value) {
- if (preg_match('#^[a-zA-Z0-9_-]+$#', $key)) {
- if (!is_array($value)) {
- $get[] = $key . '=' . $value;
- }
- else {
- foreach ((array)$value as $val) {
- if (!empty($val)) {
- $get[] = $key . '[]=' . $val;
- }
- }
- }
- }
- }
- $uri = @implode(self::URI_DELIMITER, $url);
- if (count($get) > 0) {
- $uri .= '?' . implode('&', $get);
- }
- $this->_assembled[$k] = rtrim(preg_replace_callback(self::DEFAULT_MATCH, array($this, '_defaultMatchCallback'), $this->_route)
- . self::URI_DELIMITER
- . $uri, self::URI_DELIMITER);
- }
- else {
- $this->_assembled[$k] = null;
- }
- return $this->_assembled[$k];
- }
-
- private function _defaultMatchCallback($matches)
- {
- return $this->_vars[$matches[1]];
- }
- }
|