XmlImportAstIf.php 2.89 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';
require_once dirname(__FILE__) . '/XmlImportAstElseif.php';

/**
 * Represents an IF clause
 */
class XmlImportAstIf extends XmlImportAstStatement
{
  /**
   * Condition
   *
   * @var XmlImportAstExpression
   */
  private $condition;

  /**
   * If body
   *
   * @var XmlImportAstSequence
   */
  private $ifBody;

  /**
   * List of elseif subclauses
   *
   * @var array
   */
  private $elseIfs = array();

  /**
   * Else body
   *
   * @var XmlImportAstSequence
   */
  private $elseBody;

  /**
   * Creates new instance
   *
   * @param XmlImportAstExpression $condition
   */
  public function __construct(XmlImportAstExpression $condition)
  {
    $this->condition = $condition;
  }

  /**
   * Gets condition
   *
   * @return XmlImportAstExpression
   */
  public function getCondition()
  {
    return $this->condition;
  }

  /**
   * Gets if body
   *
   * @return XmlImportAstSequence
   */
  public function getIfBody()
  {
    return $this->ifBody;
  }

  /**
   * Adds If body
   *
   * @param XmlImportAstSequence $body
   */
  public function addIfBody(XmlImportAstSequence $body)
  {
    $this->ifBody = $body;
  }

  /**
   * Gets list of elseif subcloses
   *
   * @return array
   */
  public function getElseIfs()
  {
    return $this->elseIfs;
  }

  /**
   * Gets else body
   *
   * @return XmlImportAstSequence
   */
  public function getElseBody()
  {
    return $this->elseBody;
  }

  /**
   * Adds else body
   *
   * @param XmlImportAstSequence $body
   */
  public function addElseBody(XmlImportAstSequence $body)
  {
    $this->elseBody = $body;
  }

  /**
   * Adds elseif subclause
   *
   * @param XmlImportAstElseif $elseif
   */
  public function addElseif(XmlImportAstElseif $elseif)
  {
    $this->elseIfs[] = $elseif;
  }

  /**
   * String represetation of an IF clause
   *
   * @return string
   */
  public function __toString()
  {
    $result = "--> begin " . get_class($this) . "\n";
    $result .= '  Condition: ' . $this->condition . "\n";
    $result .= "  Body:\n";
    $array = explode("\n", $this->ifBody);
    for ($i = 0; $i < count($array); $i++)
    {
      $array[$i] = '    ' . $array[$i];
    }
    $result .= implode("\n", $array) . "\n";
    foreach($this->elseIfs as $elseIf)
    {
      $array = explode("\n", $elseIf);
      for ($i = 0; $i < count($array); $i++)
      {
        $array[$i] = '' . $array[$i];
      }
      $result .= implode("\n", $array) . "\n";
    }
    if (!is_null($this->elseBody))
    {
      $result .= "  Else:\n";
      $array = explode("\n", $this->elseBody);
      for ($i = 0; $i < count($array); $i++)
      {
        $array[$i] = '    ' . $array[$i];
      }
      $result .= implode("\n", $array) . "\n";
    }
    $result .= "--> end " . get_class($this) . "\n";
    return $result;
  }
}