Location: PHPKode > scripts > Content Parser > content-parser/ContentParser/Abstract.php
<?php
/**
  * Class for parsing some text content.
  *
  * @author Nikola Posa, www.nikolaposa.in.rs
  * @license GNU General Public License
  */
abstract class ContentParser_Abstract
{
	/**
	  * Some content. Could be some multi-dimensional array or array 
	  * of objects that has some text to parse, or simply some 
	  * string that need to be parsed.
	  *
	  * @var mixed
	  */
	protected $content;
	
	/**
	  * Optional array that is telling which elements of $content 
	  * to parse, if supplied $content is array or object.
	  *
	  * @var array
	  */
	protected $elements;
	
	/**
	  * Constructor.
	  *
	  * @param mixed Content that need to be parsed.
	  * @param array Elements of $content that need to be parsed. (optional)
	  * @return ContentParser_Abstract
	  */
	public function __construct($content, $elements = array())
	{
		if (!is_array($content) && !is_string($content)) { //Only array or string is allowed.
			throw new ContentParser_Exception('Supplied content must be either array or string.');
		}
		elseif (!is_array($elements)) {
			throw new ContentParser_Exception('Second argument should be array with type and elements to parse, as key->value pairs.');
		}
		elseif (is_string($content) && !empty($elements)) {
			throw new ContentParser_Exception('You don\'t need to pass second argument, if you just want to parse some string.');
		}
		
		$this->content = $content;
		
		$this->elements = $elements;
	}
	
	/**
	  * Parses supplied content. This function uses doParseContent() 
	  * function, which is redefined in classes that extend this 
	  * abstract class.
	  *
	  * @return void
	  */
	protected function parseContent()
	{	
		if (!empty($this->elements)) { //If user choosed which elements he wants to parse.
			foreach ($this->elements as $el) {
				$this->content = $this->parseRecursive($this->content, $el);
			}
		}
		else { //No elements to parse? Parse all elements.
			$this->content = $this->parseRecursive($this->content);
		}
	}
	
	/**
	  * Recursive function that helps in parsing procedure, in cases 
	  * when user wants to parse elements of some array or object.
	  *
	  * @param string What element of array or which property of some object to parse.
	  * @return void
	  */
	protected function parseRecursive($content, $element = null)
	{
		if (is_array($content) || $content instanceof ArrayAccess) { //Array?
			if ($element != null && array_key_exists($element, $content)) {
				$content[$element] = $this->parseRecursive($content[$element], $element);
			}
			else {
				foreach ($content as $key=>$con) {
					$content[$key] = $this->parseRecursive($con, $element);
				}
			}
		}
		elseif (is_object($content)) { //Object?
			if ($element != null  && is_string($element)) {
				$content->$element = $this->parseRecursive($content->$element, $element);
			}
			else {
				foreach (get_object_vars($content) as $key=>$con) {
					$content->$key = $this->parseRecursive($con, $element);
				}
			}
		}
		elseif (is_string($content)) { //... or string?
			$content = $this->doParseContent($content);
		}
		
		return $content;
	}
	
	/**
	  * Function that does text parsing.
	  *
	  * @param string Text that will be parsed.
	  * @return string
	  */
	abstract protected function doParseContent($text);
}
?>
Return current item: Content Parser