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

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

/**
 * Instruction extractor class definition.
 * 
 * @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 IInstructionExtractor
 * @version 6.0
 * @since 5.0
 */
final class InstructionExtractor extends PAXObject implements IInstructionExtractor 
{
	/**
	 * The extraction pattern
	 *
	 * @access private
	 * @var string
	 */
	private $extractionPattern;
				
	/**
	 * Class constructor.
	 *
	 * @access public
	 */
	public function __construct()
	{
		// init the parent
		parent::__construct();
		
		// init the members
		$this->extractionPattern = null;
	}


	/**
	 * Method to extract the instructions from a string.
	 * Throws exception in case of error
	 *
	 * @access public
	 * @param string <b>$input</b> The source string
	 * @param string <b>$startDelimiter</b> The start delimiter of the expression
	 * @param string <b>$endDelimiter</b> The end delimiter of the expression
	 * @return mixed False if error encountered or array with the instructions found
	 */
	public function extract($input, $startDelimiter, $endDelimiter)
	{
		// init the extraction pattern the first time
		if (is_null($this->extractionPattern))
		{
			// translate the start delimiter from null to empty
			if (is_null($startDelimiter))
				$startDelimiter = "";

			// translate the end delimiter from null to empty
			if (is_null($endDelimiter))
				$endDelimiter = "";
			
			// create the extraction pattern
			$pattern = new Pattern();
			$this->extractionPattern = $pattern->makeInstructionSearchPattern($startDelimiter, $endDelimiter); 						
		}
		
		// try to extract the instructions from the source
		// if failed, throw exception
		if (@preg_match_all($this->extractionPattern, $input, $tempInstructions, PREG_SET_ORDER) === false)
			throw new PAXException(sprintf(Messages::$MSG_042, $input), 42);
		
		// check the instructions so far
		if (sizeof($tempInstructions) == 0)
			return false;	
			
		$instructions = array();	
		
		// since the instructions so far are a double entry array
		// use only the real part
		foreach ($tempInstructions as $instructionData)
		{
			if (!is_array($instructionData))
				continue;
				
			if (!isset($instructionData[1]))
				continue;

			array_push($instructions, $instructionData[1]);
		}
		
		// final validations
		if (sizeof($instructions) == 0)
			return false;
			
		// return the instructions
		return $instructions;
	}
	
	/**
	 * Class destructor.
	 * 
	 * @access public
	 */
	function __destruct()
	{
	}
}

?>
Return current item: PAX