File indexing completed on 2024-06-23 05:55:09

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Controller
0017  * @subpackage Router
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_Controller_Router_Route_Abstract */
0024 // require_once 'Zend/Controller/Router/Route/Abstract.php';
0025 
0026 /**
0027  * StaticRoute is used for managing static URIs.
0028  *
0029  * It's a lot faster compared to the standard Route implementation.
0030  *
0031  * @package    Zend_Controller
0032  * @subpackage Router
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Controller_Router_Route_Static extends Zend_Controller_Router_Route_Abstract
0037 {
0038 
0039     /**
0040      * Route
0041      *
0042      * @var string|null
0043      */
0044     protected $_route = null;
0045 
0046     /**
0047      * Default values for the route (ie. module, controller, action, params)
0048      *
0049      * @var array
0050      */
0051     protected $_defaults = array();
0052 
0053     /**
0054      * Get the version of the route
0055      *
0056      * @return int
0057      */
0058     public function getVersion()
0059     {
0060         return 1;
0061     }
0062 
0063     /**
0064      * Instantiates route based on passed Zend_Config structure
0065      *
0066      * @param Zend_Config $config Configuration object
0067      * @return Zend_Controller_Router_Route_Static
0068      */
0069     public static function getInstance(Zend_Config $config)
0070     {
0071         $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
0072 
0073         return new self($config->route, $defs);
0074     }
0075 
0076     /**
0077      * Prepares the route for mapping.
0078      *
0079      * @param string $route    Map used to match with later submitted URL path
0080      * @param array  $defaults Defaults for map variables with keys as variable names
0081      */
0082     public function __construct($route, $defaults = array())
0083     {
0084         $this->_route    = trim($route, self::URI_DELIMITER);
0085         $this->_defaults = (array) $defaults;
0086     }
0087 
0088     /**
0089      * Matches a user submitted path with a previously defined route.
0090      * Assigns and returns an array of defaults on a successful match.
0091      *
0092      * @param string $path Path used to match against this routing map
0093      * @return array|false An array of assigned values or a false on a mismatch
0094      */
0095     public function match($path, $partial = false)
0096     {
0097         if ($partial) {
0098             if ((empty($path) && empty($this->_route))
0099                 || (substr($path, 0, strlen($this->_route)) === $this->_route)
0100             ) {
0101                 $this->setMatchedPath($this->_route);
0102 
0103                 return $this->_defaults;
0104             }
0105         } else {
0106             if (trim($path, self::URI_DELIMITER) == $this->_route) {
0107                 return $this->_defaults;
0108             }
0109         }
0110 
0111         return false;
0112     }
0113 
0114     /**
0115      * Assembles a URL path defined by this route
0116      *
0117      * @param array $data An array of variable and value pairs used as parameters
0118      * @return string Route path with user submitted parameters
0119      */
0120     public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
0121     {
0122         return $this->_route;
0123     }
0124 
0125     /**
0126      * Return a single parameter of route's defaults
0127      *
0128      * @param string $name Array key of the parameter
0129      * @return string Previously set default
0130      */
0131     public function getDefault($name)
0132     {
0133         if (isset($this->_defaults[$name])) {
0134             return $this->_defaults[$name];
0135         }
0136 
0137         return null;
0138     }
0139 
0140     /**
0141      * Return an array of defaults
0142      *
0143      * @return array Route defaults
0144      */
0145     public function getDefaults()
0146     {
0147         return $this->_defaults;
0148     }
0149 }