Location: PHPKode > scripts > PAX > pax/core/classes/Pattern.class.php
<?php


/**
 * Page DocBlock definition
 * @package org.zadara.marius.pax
 */

/**
 * Pattern class definition. 
 * This class role is to provide basic pattern generation
 * used to extract an replace standard instructions from source file.
 * 
 * @author Marius Zadara <hide@address.com>
 * @category Classes
 * @copyright (C) 2008-2009 Marius Zadara
 * @license Free for non-comercial use
 * @package org.zadara.marius.pax
 * @final 
 * @see PAXObject
 * @see IPattern
 * @version 6.0
 * @since 6.0 
 */
final class Pattern extends PAXObject implements IPattern  
{
	/**
	 * List of chars that need escaping.
	 *
	 * @access private
	 * @var array
	 */
	private $charsToEscape;
	
	/**
	 * Chars escaped.
	 *
	 * @access private
	 * @var array
	 */
	private $escapedChars;

	/**
	 * Pattern start delimiter.
	 *
	 * @access private
	 * @var string
	 */
	private $startDelimiter;
	
	/**
	 * Pattern end delimiter.
	 *
	 * @access private
	 * @var string
	 */
	private $endDelimiter;
	
	/**
	 * The global pattern.
	 * 
	 * @access private
	 * @var string
	 */
	private $pattern;
	
	/**
	 * Class constructor.
	 * 
	 * @access public
	 */
	public function __construct()
	{
		// init the parent
		parent::__construct();
		
		// init the members
		$this->charsToEscape = null;
		$this->escapedChars = null;		
		
		$this->startDelimiter = null;
		$this->endDelimiter = null;
		
		// set the default pattern
		// the _BODY_ part will be replaced with usefull string later
		$this->pattern = "/%s\s*_BODY_\s*%s/U";
	}
	
	/**
	 * Method to make the instructions search pattern.
	 *
	 * @access public
	 * @param string <b>$startDelimiter</b> The pattern's start delimiter
	 * @param string <b>$endDelimiter</b> The pattern's end delimiter
	 * @return string
	 */
	public function makeInstructionSearchPattern($startDelimiter, $endDelimiter)
	{
		// validate the start delimiter
		// and set only the first time
		if (is_null($this->startDelimiter))
		{
			// since regular expressions will be used, 
			// escape the delimiter
			$this->startDelimiter = $this->escape($startDelimiter);
		}
			
		// the same thing for the end delimiter
		if (is_null($this->endDelimiter))
			$this->endDelimiter = $this->escape($endDelimiter);

		// replace the body part from the global pattern
		$pattern = str_ireplace("_BODY_", "(.+;)", $this->pattern);
			
		// add the start and end pattern to the global pattern
		return sprintf($pattern, $this->startDelimiter, $this->endDelimiter); 						
	}
	
	
	/**
	 * Method to create the instruction replacement pattern.
	 *
	 * @access public
	 * @param string <b>$instruction</b> The instruction search for
	 * @param string <b>$startDelimiter</b> The pattern's start delimiter 
	 * @param string <b>$endDelimiter</b> The pattern's end delimiter
	 * @return string
	 */
	public function makeInstructionReplacePattern($instruction, $startDelimiter, $endDelimiter)
	{
		// validate the start delimiter
		if (is_null($this->startDelimiter))
		{
			// since regular expressions will be used, 
			// escape the delimiter			
			$this->startDelimiter = $this->escape($startDelimiter);
		}
			
		// and the end delimiter
		if (is_null($this->endDelimiter))
			$this->endDelimiter = $this->escape($endDelimiter);
		
		// replace the body part from the global pattern
		// with the escaped instruction
		$pattern = str_ireplace("_BODY_", $this->escape($instruction), $this->pattern);	
		
		// return the new pattern
		return sprintf($pattern, $this->startDelimiter, $this->endDelimiter);
	}
	
	
	/**
	 * Method to escape custom characters.
	 * This method will escape characters that have regullar expression meaning.
	 *
	 * @access private
	 * @param string <b>$string</b> The input string, where the characters will be escaped
	 * @return string 
	 */
	private function escape($string)
	{
		// escape the characters only the first time
		if (!is_array($this->escapedChars))
		{
			// the character needed for escaping
			$escapeChar = "\\";
			
			$this->escapedChars = array();
			
			// the custom characters
			$this->charsToEscape = str_split("?.,+-*()[]{}");
			
			// add the escaped characters to the new array
			foreach ($this->charsToEscape as $charToEscape)
				array_push($this->escapedChars, $escapeChar.$charToEscape);		
		}
		
		// replace the characters with the escaped one in the string
		return str_ireplace($this->charsToEscape, $this->escapedChars, $string);
	}
	
	/**
	 * Class destuctor
	 *
	 * @access private
	 */
	function __destruct()
	{	
	}
}

?>
Return current item: PAX