// /** * Stores XMLNS aliases fluctuation in the xml flow. * * This class is used to bind a PHPTAL namespace to an alias, for example using * xmlns:t="http://xml.zope.org/namespaces/tal" and later use t:repeat instead * of tal:repeat. * * @package phptal.dom * @author Laurent Bedubourg */ class PHPTAL_Dom_XmlnsState { /** Create a new XMLNS state inheriting provided aliases. */ public function __construct($aliases = array()) { assert(is_array($aliases)); $this->_aliases = $aliases; } /** Returns true if $attName is a valid attribute name, false otherwise. */ public function isValidAttribute($attName) { $unaliased = $this->unAliasAttribute($attName); return PHPTAL_Dom_Defs::getInstance()->isValidAttribute($unaliased); } /** Returns true if $attName is a PHPTAL attribute, false otherwise. */ public function isPhpTalAttribute($attName) { $unaliased = $this->unAliasAttribute($attName); return PHPTAL_Dom_Defs::getInstance()->isPhpTalAttribute($unaliased); } /** Returns the unaliased name of specified attribute. */ public function unAliasAttribute($attName) { if (count($this->_aliases) == 0) return $attName; $result = $attName; foreach ($this->_aliases as $alias => $real){ $result = str_replace("$alias:", "$real:", $result); } return $result; } /** * Returns a new XmlnsState inheriting of $currentState if $nodeAttributes contains * xmlns attributes, returns $currentState otherwise. * * This method is used by the PHPTAL parser to keep track of xmlns fluctuation for * each encountered node. */ public static function newElement(PHPTAL_Dom_XmlnsState $currentState, $nodeAttributes) { $aliases = array(); foreach ($nodeAttributes as $att => $value){ if (PHPTAL_Dom_Defs::getInstance()->isHandledXmlNs($att, $value)){ preg_match('/^xmlns:(.*?)$/', $att, $m); list(,$alias) = $m; $aliases[$alias] = PHPTAL_Dom_Defs::getInstance()->xmlnsToLocalName($value); } } if (count($aliases) > 0){ // inherit aliases with maybe an overwrite $aliases = array_merge($currentState->_aliases, $aliases); return new PHPTAL_Dom_XmlnsState($aliases); } return $currentState; } private $_aliases; } ?>