Hoa central
Compiler.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Ruler\Visitor;
38 
39 use Hoa\Core;
40 use Hoa\Ruler;
41 use Hoa\Visitor;
42 
51 class Compiler implements Visitor\Visit
52 {
58  protected $_indentation = 0;
59 
60 
61 
70  public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
71  {
72  $out = null;
73  $_ = str_repeat(' ', $this->_indentation);
74 
75  if ($element instanceof Ruler\Model) {
76  $this->_indentation = 1;
77  $out =
78  '$model = new \Hoa\Ruler\Model();' . "\n" .
79  '$model->expression =' . "\n" .
80  $element->getExpression()->accept($this, $handle, $eldnah) .
81  ';';
82  } elseif ($element instanceof Ruler\Model\Operator) {
83  $out = $_ . '$model->';
84  $name = $element->getName();
85  $_handle = [];
86 
87  if (false === $element->isFunction()) {
88  if (true === Core\Consistency::isIdentifier($name)) {
89  $out .= $name;
90  } else {
91  $out .= '{\'' . $name . '\'}';
92  }
93 
94  $out .= '(' . "\n";
95  } else {
96  $out .= 'func(' . "\n" . $_ . ' ';
97  $_handle[] = '\'' . $name . '\'';
98  }
99 
101 
102  foreach ($element->getArguments() as $argument) {
103  $_handle[] = $argument->accept($this, $handle, $eldnah);
104  }
105 
107 
108  $out .= implode(',' . "\n", $_handle) . "\n" . $_ . ')';
109  } elseif ($element instanceof Ruler\Model\Bag\Scalar) {
110  $value = $element->getValue();
111  $out = $_;
112 
113  if (true === $value) {
114  $out .= 'true';
115  } elseif (false === $value) {
116  $out .= 'false';
117  } elseif (null === $value) {
118  $out .= 'null';
119  } elseif (is_numeric($value)) {
120  $out .= (string) $value;
121  } else {
122  $out .= '\'' . str_replace('\\', '\\\'', $value) . '\'';
123  }
124  } elseif ($element instanceof Ruler\Model\Bag\RulerArray) {
125  $values = [];
127 
128  foreach ($element->getArray() as $value) {
129  $values[] = $value->accept($this, $handle, $eldnah);
130  }
131 
133  $out =
134  $_ . '[' . "\n" .
135  implode(',' . "\n", $values) . "\n" .
136  $_ . ']';
137  } elseif ($element instanceof Ruler\Model\Bag\Context) {
138  $out = $_ . '$model->variable(\'' . $element->getId() . '\')';
139  $this->_indentation += 2;
140 
141  foreach ($element->getDimensions() as $dimension) {
142  $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
143  $out .= "\n" . $_ . ' ->';
144 
145  switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) {
146  case Ruler\Model\Bag\Context::ARRAY_ACCESS:
147  $out .=
148  'index(' . "\n" .
149  $value->accept($this, $handle, $eldnah) . "\n" .
150  $_ . ' )';
151 
152  break;
153 
154  case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS:
155  $out .= 'attribute(\'' . $value . '\')';
156 
157  break;
158 
159  case Ruler\Model\Bag\Context::METHOD_ACCESS:
160  $out .=
161  'call(' . "\n" .
162  $value->accept($this, $handle, $eldnah) . "\n" .
163  $_ . ' )';
164 
165  break;
166  }
167  }
168 
169  $this->_indentation -= 2;
170  }
171 
172  return $out;
173  }
174 }
visit(Visitor\Element $element, &$handle=null, $eldnah=null)
Definition: Compiler.php:70