XmlImportAstSequence.php 2.85 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
<?php
/**
 * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com>
 * @package AST
 */

require_once dirname(__FILE__) . '/XmlImportAstStatement.php';

/**
 * Represents statement sequence
 */
class XmlImportAstSequence extends XmlImportAstStatement
{

  /**
   * Sequence instance number
   *
   * @var int
   */
  private static $sequenceInstanceNumber = 0;

  /**
   * Current sequence number
   *
   * @var int
   */
  private $sequenceNumber;

  /**
   * Current variable number
   *
   * @var int 
   */
  private $varNumber = 0;
  
  /**
   * List of statements
   *
   * @var array
   */
  private $statements = array();

  /**
   * Variable definitions
   *
   * @var array
   */
  private $variableDefinitions = array();

  /**
   * Variables
   *
   * @var array
   */
  private $variables = array();

  /**
   * Creates new instance
   */
  public function __construct()
  {
    $this->sequenceNumber = self::$sequenceInstanceNumber++;
  }

  /**
   * Adds statement to a sequence
   *
   * @param XmlImportAstStatement $statement
   */
  public function addStatement(XmlImportAstStatement $statement)
  {
    $this->statements[] = $statement;
  }

  /**
   * Gets the list of statements
   *
   * @return array
   */
  public function getStatements()
  {
    return $this->statements;
  }

  /**
   * Adds variable to a sequence
   *
   * @param XmlImportAstXPath $xpath
   */
  public function addVariable(XmlImportAstXPath $xpath)
  {
    if (!array_key_exists($xpath->getValue(), $this->variables))
    {
      $var = '$v' . $this->sequenceNumber . '_' . $this->varNumber++;
      $value =     $escapedValue = strtr($xpath->getValue(), array(
    	"\n" => "\\n",
    	"\t" => "\\t",
    	"\r" => "\\r",
    	"$" => "\\$",
    	"\"" => "\\\"",
    	"\\" => "\\\\",
      ));
      $result = $var . ' = {{XML}}->xpath("' . $value . '");';
      $this->variables[$xpath->getValue()] = $var;
      $this->variableDefinitions[] = $result;
    }
  }

  /**
   * Gets variable definitions
   *
   * @return array
   */
  public function getVariableDefinitions()
  {
    return $this->variableDefinitions;
  }

  /**
   * Gets variables
   *
   * @return array
   */
  public function getVariables()
  {
    return $this->variables;
  }

  /**
   * Returns the number of current instance
   *
   * @return int
   */
  public function getSequenceNumber()
  {
    return $this->sequenceNumber;
  }

  /**
   * String representation of a sequence node
   *
   * @return string
   */
  public function __toString()
  {
    $result = "--> begin " . get_class($this) . "\n";
    foreach ($this->getStatements() as $statement)
    {
      $array = explode("\n", $statement);
      for ($i = 0; $i < count($array); $i++)
      {
        $array[$i] = '  ' . $array[$i];
      }
      $result .= implode("\n", $array) . "\n";
    }

    $result .= "--> end " . get_class($this);

    return $result;
  }
}