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

/**
 * Makes token objects using some text as instructions
 */
class Template_TokenFactory {

    /**
     * The text to search in
     * @var String
     */
    protected $text;
    
    /**
     * The tokens found
     * @var Template_Token[]
     */
    protected $tokens;

    /**
     * Find the tokens in the text
     * @param String $text
     * @return Template_Token[]
     */
    public function tokenize($text) {
        $this->text = $text;
        $this->find_tokens_in_text();
        return count($this->tokens) > 0 ? $this->tokens : false;
    }

    /**
     * Helper function to do the searching for tokens, automagically handles 
     * found tokens
     * 
     * @param String $text Include to override internal text 
     */
    protected function find_tokens_in_text($text=NULL) {
        $regex =
                '(?i)' .
                '\{' .
                '[ ]*' .
                '(' .
                '[\w_-]+' .
                '(?:' .
                '\.' .
                '[\w_-]+' .
                ')*' .
                ')' .
                '(?:[\w\s_"\'=-]|(?R))*' .
                '\}';
        preg_replace_callback("/$regex/", array($this, "handle_tokens"), $text == NULL ? $this->text : $text);
    }

    /**
     * Recieves an array from the preg search, builds tokens from information
     * 
     * @param String[] $matches 
     */
    protected function handle_tokens($matches) {
        $children = array();
        // Do inner-tokens first
        $tokensbefore = count($this->tokens);
        $this->find_tokens_in_text(strlen($matches[0]) > 2 ? substr($matches[0], 1, -1) : " ");
        if ($tokensbefore < count($this->tokens)) {
            $children = array_slice($this->tokens,$tokensbefore);
            $this->tokens = array_slice($this->tokens,0,($tokensbefore));            
        }
        $this->tokens[] = new Template_Token($matches[0],$matches[1],$children);
    }

}

?>
Return current item: Blackwolf CMF