* @since 1.9 */ class CmsRoute implements ArrayAccess { private $_data; private $_results; static private $_keys = array('term','key1','key2','key3','defaults','absolute','results'); /** * Construct a new route object. * * @param string The route string (or regular expression) * @param mixed The first key. Usually a module name. * @param array An array of parameter defaults for this module. Only applicable when the destination is a module. * @param boolean Flag indicating wether the term is a regular expression or an absolute string. * @param string The second key. */ public function __construct($term,$key1 = '',$defaults = '',$is_absolute = FALSE,$key2 = null,$key3 = null) { $this->_data['term'] = $term; $this->_data['absolute'] = $is_absolute; if( is_numeric($key1) && empty($key2) ) { $this->_data['key1'] = '__CONTENT__'; $this->_data['key2'] = (int)$key1; } else { $this->_data['key1'] = $key1; $this->_data['key2'] = $key2; } if( is_array($defaults) ) { $this->_data['defaults'] = $defaults; } if( !empty($key3) ) { $this->_data['key3'] = $key3; } } public static function &new_builder($term,$key1,$key2 = '',$defaults = '',$is_absolute = FALSE,$key3 = null) { $obj = new CmsRoute($term,$key1,$defaults,$is_absolute,$key2,$key3); return $obj; } public function signature() { $tmp = serialize($this->_data); $tmp = md5($tmp); return $tmp; } public function OffsetGet($key) { if( in_array($key,self::$_keys) && isset($this->_data[$key]) ) { return $this->_data[$key]; } } public function OffsetSet($key,$value) { if( in_array($key,self::$_keys) ) { $this->_data[$key] = $value; } } public function OffsetExists($key) { if( in_array($key,self::$_keys) && isset($this->_data[$key]) ) { return TRUE; } return FALSE; } public function OffsetUnset($key) { if( in_array($key,self::$_keys) && isset($this->_data[$key]) ) { unset($this->_data[$key]); } } /** * Returns the route term (string or regex) * * @deprecated */ public function get_term() { return $this->_term; } /** * Retrieve the destination module name. * * @return string Destination module name. or null. * @deprecated */ public function get_dest() { return $this->_data['key1']; } /** * Retrieve the page id, if the destination is a content page. * * @return integer Page id, or null. * @deprecated */ public function get_content() { if( $this->is_content() ) return $this->_data['key2']; } /** * Retrieve the default parameters for this route * * @return array The default parameters for the route.. Null if no defaults specified. * @deprecated */ public function get_defaults() { if( isset($this->_data['defaults']) ) return $this->_data['defaults']; } /** * Test wether this route is for a page. * * @return boolean * @deprecated */ public function is_content() { return ($this->_data['key1'] == '__CONTENT__')?TRUE:FALSE; } /** * Get matching parameter results. * * @return array Matching parameters... or Null * @deprecated */ public function get_results() { return $this->_results; } /** * Test if this route matches the specified string * Depending upon the route, either a string comparison or regular expression match * is performed. * * @param string The input string * @param boolean Perform an exact string match rather than depending on the route values. * @param array Optional array which will contain output matched parameters for regular expression * searches. * @return boolean */ public function matches($str,$exact = false) { $this->_results = null; if( (isset($this->_data['absolute']) && $this->_data['absolute']) || $exact ) { $a = trim($this->_data['term']); $a = trim($a,'/'); $b = trim($str); $b = trim($b,'/'); if( !strcasecmp($a,$b) ) return TRUE; return FALSE; } // regular expression matches. $tmp = array(); $res = (bool)preg_match($this->_data['term'],$str,$tmp); if( $res && is_array($tmp) ) $this->_results = $tmp; return $res; } } // end of class # vim:ts=4 sw=4 noet ?>