XmlImportAstElseif.php
14 years ago
XmlImportAstExpression.php
14 years ago
XmlImportAstFloat.php
14 years ago
XmlImportAstForeach.php
14 years ago
XmlImportAstFunction.php
14 years ago
XmlImportAstIf.php
14 years ago
XmlImportAstInteger.php
14 years ago
XmlImportAstLiteral.php
14 years ago
XmlImportAstMath.php
14 years ago
XmlImportAstPrint.php
14 years ago
XmlImportAstSequence.php
14 years ago
XmlImportAstStatement.php
14 years ago
XmlImportAstString.php
14 years ago
XmlImportAstText.php
14 years ago
XmlImportAstWith.php
14 years ago
XmlImportAstXPath.php
14 years ago
XmlImportAstXpathClause.php
14 years ago
XmlImportAstSequence.php
161 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com> |
| 4 | * @package AST |
| 5 | */ |
| 6 | |
| 7 | require_once dirname(__FILE__) . '/XmlImportAstStatement.php'; |
| 8 | |
| 9 | /** |
| 10 | * Represents statement sequence |
| 11 | */ |
| 12 | class XmlImportAstSequence extends XmlImportAstStatement |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * Sequence instance number |
| 17 | * |
| 18 | * @var int |
| 19 | */ |
| 20 | private static $sequenceInstanceNumber = 0; |
| 21 | |
| 22 | /** |
| 23 | * Current sequence number |
| 24 | * |
| 25 | * @var int |
| 26 | */ |
| 27 | private $sequenceNumber; |
| 28 | |
| 29 | /** |
| 30 | * Current variable number |
| 31 | * |
| 32 | * @var int |
| 33 | */ |
| 34 | private $varNumber = 0; |
| 35 | |
| 36 | /** |
| 37 | * List of statements |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | private $statements = array(); |
| 42 | |
| 43 | /** |
| 44 | * Variable definitions |
| 45 | * |
| 46 | * @var array |
| 47 | */ |
| 48 | private $variableDefinitions = array(); |
| 49 | |
| 50 | /** |
| 51 | * Variables |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | private $variables = array(); |
| 56 | |
| 57 | /** |
| 58 | * Creates new instance |
| 59 | */ |
| 60 | public function __construct() |
| 61 | { |
| 62 | $this->sequenceNumber = self::$sequenceInstanceNumber++; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Adds statement to a sequence |
| 67 | * |
| 68 | * @param XmlImportAstStatement $statement |
| 69 | */ |
| 70 | public function addStatement(XmlImportAstStatement $statement) |
| 71 | { |
| 72 | $this->statements[] = $statement; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Gets the list of statements |
| 77 | * |
| 78 | * @return array |
| 79 | */ |
| 80 | public function getStatements() |
| 81 | { |
| 82 | return $this->statements; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Adds variable to a sequence |
| 87 | * |
| 88 | * @param XmlImportAstXPath $xpath |
| 89 | */ |
| 90 | public function addVariable(XmlImportAstXPath $xpath) |
| 91 | { |
| 92 | if (!array_key_exists($xpath->getValue(), $this->variables)) |
| 93 | { |
| 94 | $var = '$v' . $this->sequenceNumber . '_' . $this->varNumber++; |
| 95 | $value = $escapedValue = strtr($xpath->getValue(), array( |
| 96 | "\n" => "\\n", |
| 97 | "\t" => "\\t", |
| 98 | "\r" => "\\r", |
| 99 | "$" => "\\$", |
| 100 | "\"" => "\\\"", |
| 101 | "\\" => "\\\\", |
| 102 | )); |
| 103 | $result = $var . ' = {{XML}}->xpath("' . $value . '");'; |
| 104 | $this->variables[$xpath->getValue()] = $var; |
| 105 | $this->variableDefinitions[] = $result; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Gets variable definitions |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | public function getVariableDefinitions() |
| 115 | { |
| 116 | return $this->variableDefinitions; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Gets variables |
| 121 | * |
| 122 | * @return array |
| 123 | */ |
| 124 | public function getVariables() |
| 125 | { |
| 126 | return $this->variables; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the number of current instance |
| 131 | * |
| 132 | * @return int |
| 133 | */ |
| 134 | public function getSequenceNumber() |
| 135 | { |
| 136 | return $this->sequenceNumber; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * String representation of a sequence node |
| 141 | * |
| 142 | * @return string |
| 143 | */ |
| 144 | public function __toString() |
| 145 | { |
| 146 | $result = "--> begin " . get_class($this) . "\n"; |
| 147 | foreach ($this->getStatements() as $statement) |
| 148 | { |
| 149 | $array = explode("\n", $statement); |
| 150 | for ($i = 0; $i < count($array); $i++) |
| 151 | { |
| 152 | $array[$i] = ' ' . $array[$i]; |
| 153 | } |
| 154 | $result .= implode("\n", $array) . "\n"; |
| 155 | } |
| 156 | |
| 157 | $result .= "--> end " . get_class($this); |
| 158 | |
| 159 | return $result; |
| 160 | } |
| 161 | } |