Location: PHPKode > projects > Blackwolf CMF > projectBlackWolf/lib/Template/Token.class.php
<?php
/* Please see the README and LICENSE files. */

/**
 * This represents some token found in the text, it's type, parameters, and if
 * it has sub-tokens which need to be done first
 */
class Template_Token {
    
    ////////////////////////////////////////////////////////////////////////////
    // START CONFIGURABLE     
    
    /**
     * What to replace with if parsing fails
     * @var String
     */
    public static $default = "Unparsed Token";
    
    /**
     * A mapping of token prefixes and the class to use with them
     * @var String[]
     */
    protected static $parser_map = array(
        "model"=>"Template_TokenParser_Model",
        "var"=>"Template_TokenParser_Var",
        "lang"=>"Template_TokenParser_Lang",
	"server"=>"Template_TokenParser_Server",
	"tool"=>"Template_TokenParser_Tool",
        "page"=>"Template_TokenParser_Page"
    );
    
    // END CONFIGURABLE
    ////////////////////////////////////////////////////////////////////////////
    
    /**
     * The text that represents thhis token
     * @var String
     */
    protected $text;
    
    /**
     * The data in the type field
     * Ex.
     *   model.attribute.value would become
     *   String(3)["model","attribute","value"]
     * 
     * @var String[]
     */
    protected $type;
    
    /**
     * The text that was the result of parsing
     * @var String
     */
    protected $parsed;
    
    /**
     * The parameters of the token
     * Ex.
     *   name="foo" bar="bar" becomes
     *   String(2)["name=\"foo\"","bar=\"bar\""]
     * 
     * @var String[]
     */
    protected $params;
    
    /**
     * Any children
     * @var Template_Token[]
     */
    protected $children;
    
    /**
     * Create a new template token
     * 
     * @param String $token_text
     * @param String[] $token_type
     * @param Template_Token[] $children 
     */
    public function __construct($token_text,$token_type,$children=array()){
        $this->text = $token_text;
        $this->type = explode(".",$token_type);
        $this->children = $children;
        $this->find_params();
    }
    
    /**
     * Find all of the parameters of the token, and save them
     */
    public function find_params(){
        $regex = "(?i)([\w_\{\}.-]+[ ]?=((\"|')[\w\s_\{\}.-]*\g{-1}|[\w_\{\}.-]+))";
        $a = preg_match_all("/$regex/", strlen($this->text) > 2 ? 
                                            substr($this->text, 1, -1) :
                                            " ", $args);
        $this->params = $a>0?$args[0]:NULL;
    }
    
    /**
     * Use the correct parser to interpret the token
     * 
     * @param String[] $vars
     * @param Data_Model $model 
     */
    public function parse($vars=NULL,$model=NULL){
        $this->parsed = $this->text;
        $this->parse_children($vars,$model);
        if(array_key_exists(strtolower($this->type[0]),self::$parser_map)){
            $p = new self::$parser_map[strtolower($this->type[0])];
            $this->parsed = $p->parse($this->type,$this->params,$vars,$model);
        } else {
            $this->parsed = self::$default." (".$this->type[0].")";
            trigger_error(  "Unable to parse token of type ".$this->type[0],
                            E_USER_WARNING);
        }        
    }
    
    /**
     * Instruct sub_tokens to parse
     * 
     * @param String[] $vars
     * @param Data_Model $model 
     */
    protected function parse_children($vars,$model){
        if(count($this->children)>0){            
            foreach($this->children as $child){
                $child->parse($vars,$model);
                $this->parsed = str_replace($child->text,
                                            $child->parsed,
                                            $this->parsed);
            }
        }
    }
    
    
    public function __get($name){
        if($name=="text"){return $this->text;}
        if($name=="parsed"){return $this->parsed;}
    }
}

?>
Return current item: Blackwolf CMF