123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- /**
- *
- * Cube Framework $Id$ cOvGSa9QPt4189rmC7aLgem5hXPRB+LLhHrBiSlt62c=
- *
- * @link http://codecu.be/framework
- * @copyright Copyright (c) 2014 CodeCube SRL
- * @license http://codecu.be/framework/license Commercial License
- *
- * @version 1.2
- */
- /**
- * route object when mod rewrite or equivalent is not available
- */
- namespace Cube\Controller\Router\Route;
- use Cube\Controller\Router\Standard as StandardRouter;
- class Standard extends AbstractRoute
- {
- /**
- *
- * set route default parameters
- * if there are keys in the defaults array that are different than controller and action,
- * then add these as params
- *
- * @param array $defaults
- *
- * @return $this
- */
- public function setDefaults(array $defaults)
- {
- foreach ($defaults as $key => $value) {
- $this->setParam($key, $value);
- }
- $this->_defaults = $defaults;
- return $this;
- }
- /**
- *
- * get all params
- *
- * @return array
- */
- public function getAllParams()
- {
- $params['module'] = $this->getModule();
- $params['controller'] = $this->getController();
- $params['action'] = $this->getAction();
- $params += array_filter($this->getParams());
- return $params;
- }
- /**
- *
- * get an array of params and if the route matches, return a routed url
- * the method will also route requests if all params in the route match the params in
- * the request, but there are extra params in the request
- *
- * @param array $params
- * @param bool $named if the flag is set to true, we need to match by params only
- *
- * @return string|null the assembled url or null if the route doesnt match
- */
- public function assemble($params, $named = false)
- {
- if (!is_array($params)) {
- $params = (array)$params;
- }
- $allParams = $this->getAllParams() + array_filter($params);
- foreach ($params as $key => $value) {
- $allParams[$key] = $value;
- }
- $params = $allParams;
- $get = array();
- foreach ((array)$params as $key => $value) {
- if (preg_match('#^[a-zA-Z0-9_-]+$#', $key)) {
- if (!is_array($value)) {
- if (in_array($key, array('module', 'controller', 'action'))) {
- $value = $this->normalize($value, true);
- }
- $get[] = $key . '=' . $value;
- }
- else {
- foreach ((array)$value as $val) {
- if (!empty($val)) {
- $get[] = $key . '[]=' . $val;
- }
- }
- }
- }
- }
- $uri = StandardRouter::DEFAULT_PATH;
- if (count($get) > 0) {
- $uri .= '?' . implode('&', $get);
- }
- return $uri;
- }
- }
|