Location: PHPKode > scripts > Simple Tags > simple-tags/classes/Parser/Parser.php
<?php
/**
 * SimpleTags
 *
 * This class implements a simple and transparent replacement tags in the text.
 * Template: [tag].
 * 
 * @package SimpleTags
 * @author Bogdan Thehansky <hide@address.com>
 */
require_once 'classes/Parser/IParser.php';

if (! class_exists('Parser', FALSE)) {

/*
 * Find and replacement tags in the data
 * 
 */

class Parser implements IParser {
	
	/**
	 * @var string 
	 */
	protected $_parsingData = NULL;
	
	/**
	 * @var array
	 */
	private $_convertedTags;
	
	public function __construct() {
		
	}
	
	/**
	 * Sets the array of data
	 * 
	 * @param string $data
	 * @return array $this->_parsingData
	 */
	public function setData($data) {
		if ( $this->processingErrors($data) ) {
			return $this->_parsingData = $data;
		}
	}
	
	/**
	 * Return search result
	 * 
	 * @return array foundTags
	 */
	public function getFoundTags() {
		return $this->search();
	}
	
	/**
	 * Return array of the values after tags was convert
	 * 
	 * @return array convertedTags
	 */
	public function getConvertedTags() {
		return $this->tagsConverter();
	}
	
	/**
	 * Processing errors in the data 
	 * 
	 * @param string data
	 * @throws Exception $e
	 * @return boolean
	 */
	protected function processingErrors($data) {
		    if ( empty($data) ) {
				throw new Exception('Parser::$data must be not empty');
			} 
			else if (! is_string($data) ) {
				throw new Exception('Parser::$data must be a string format');
			}
			
		return true;
	}
	
	/**
	 * Finds tags in a row on the template
	 * 
	 * @return array $matches[0]
	 */
	private function search() {
		$tagsPatern = "/(\[([a-zA-Z0-9])+\])/i";
		
		try {
			preg_match_all($tagsPatern, $this->_parsingData, $matches);
		} catch(Exception $e) {
			return $this->_parsingData;
		}
		
		return $matches[0];
	}
	
	/**
	 * Converting value from the tag
	 * 
	 * @return array $this->_convertedTags
	 */
	private function tagsConverter() {
		$this->_convertedTags = array();
		$foundResult = $this->getFoundTags();
		
		foreach($foundResult as $tags) {
			$tags = str_replace('[', '', $tags);
			$tags = str_replace(']', '', $tags);
			$this->_convertedTags[] = $tags;
		}
		
		return $this->_convertedTags;
	}
	
	/**
	 * Return modify data after parsing tags
	 * 
	 * @return string $this->_parsingData
	 */
	public function parseResult() {
		$key = $this->getConvertedTags();
		$list = $this->parseTagsList();
		
		foreach ($key as $tag){
			if(array_key_exists($tag, $list)) {
				$this->_parsingData  = str_replace('[' . $tag . ']', $list[$tag], $this->_parsingData);
			}
		}
		
		return $this->_parsingData;
	}

	protected function parseTagsList() {
		require_once 'classes/Parser/TagsConfig.php';
		return TagsConfig::$tagsValue;
	}
}

}
?>
Return current item: Simple Tags