Location: PHPKode > projects > SkullPuppy > Project/library/SP/Route.php
<?php

class SP_Route {
	private $_definition, $_map, $_count, $_wild, $_uri;

	public function __construct($definition, $data) {
		if(!is_array($data)) throw new SP_Exception(
			'SP_Route second parameter must be of type "Array".'
		);
		if(substr($definition,0,1)=='/') $definition = substr($definition,1);
		if(substr($definition,-1)=='/') $definition = substr($definition,0,-1);
		if(substr($definition,-1)=='*') {
			$definition = substr($definition,0,-2);
			$this->_wild = true;
		}
		$this->_wild = (bool) $this->_wild;
		$this->_definition = explode('/', $definition);
		$this->_map = array();
		foreach($data as $key => $val) {
			$this->_map[$key] = $val;
		}
		$this->_count = count($this->_definition);
	}

	public function count() {
		return $this->_count;
	}

	public function hasWild() {
		return (bool) $this->_wild;
	}

	public function getDefinition() {
		return $this->_definition;
	}

	public function match($uri) {
		$i = 0;
		//var_dump($this->_definition);
		foreach($this->_definition as $key) {
			//print "-$key {$uri[$i]}<br/>\n";
			if(substr($key,0,1) != ':' && $key != $uri[$i])return false;
			$i++;
		}
		$this->_uri = $uri;
		return true;
	}

	public function dispatch() {
		$i = 0;
		$max = count($this->_definition);
		$param = '';
		foreach($this->_uri as $seg) {
			if($i < $max) {
				if(substr($this->_definition[$i],0,1) == ':') {
					$key = substr($this->_definition[$i],1);
					if($key == 'module' || $key == 'controller' ||
							$key == 'action') {
						$$key = $seg;
					}else{
						$params[$key] = $seg;
					}
				}
			}else{
				if($this->_wild) {
					if(!$param) $param = $seg;
					else {
						$params[$param] = $seg;
						$param = '';
						}
				}
			}
			$i++;
		}
		if($param) $params[$param] = '';
		foreach($this->_map as $key => $val) {
			switch($key) {
				case 'module':
				case 'controller':
				case 'action':
					$$key = $val;
					break;
				default:
					$params[$key] = $val;
			}
		}
		if(!$module && SP_Registry::yesnoToBool(
				SP_Registry::getKey('force_default_module')))
			$module = 'default';
		return array(
			'module' => $module,
			'controller' => $controller,
			'action' => $action,
			'params' => $params
		);
	}
}

?>
Return current item: SkullPuppy