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