/** * Visit an element. * * @param \Hoa\Visitor\Element $element Element to visit. * @param mixed &$handle Handle (reference). * @param mixed $eldnah Handle (not reference). * @return mixed */ public function visit(Visitor\Element $element, &$handle = null, $eldnah = null) { $out = null; $_ = str_repeat(' ', $this->_indentation); if ($element instanceof Ruler\Model) { $this->_indentation = 1; $out = '$model = new \\Hoa\\Ruler\\Model();' . "\n" . '$model->expression =' . "\n" . $element->getExpression()->accept($this, $handle, $eldnah) . ';'; } elseif ($element instanceof Ruler\Model\Operator) { $out = $_ . '$model->'; $name = $element->getName(); $_handle = []; if (false === $element->isFunction()) { if (true === Core\Consistency::isIdentifier($name)) { $out .= $name; } else { $out .= '{\'' . $name . '\'}'; } $out .= '(' . "\n"; } else { $out .= 'func(' . "\n" . $_ . ' '; $_handle[] = '\'' . $name . '\''; } ++$this->_indentation; foreach ($element->getArguments() as $argument) { $_handle[] = $argument->accept($this, $handle, $eldnah); } --$this->_indentation; $out .= implode(',' . "\n", $_handle) . "\n" . $_ . ')'; } elseif ($element instanceof Ruler\Model\Bag\Scalar) { $value = $element->getValue(); $out = $_; if (true === $value) { $out .= 'true'; } elseif (false === $value) { $out .= 'false'; } elseif (null === $value) { $out .= 'null'; } elseif (is_numeric($value)) { $out .= (string) $value; } else { $out .= '\'' . str_replace('\\', '\\\'', $value) . '\''; } } elseif ($element instanceof Ruler\Model\Bag\RulerArray) { $values = []; ++$this->_indentation; foreach ($element->getArray() as $value) { $values[] = $value->accept($this, $handle, $eldnah); } --$this->_indentation; $out = $_ . '[' . "\n" . implode(',' . "\n", $values) . "\n" . $_ . ']'; } elseif ($element instanceof Ruler\Model\Bag\Context) { $out = $_ . '$model->variable(\'' . $element->getId() . '\')'; $this->_indentation += 2; foreach ($element->getDimensions() as $dimension) { $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE]; $out .= "\n" . $_ . ' ->'; switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) { case Ruler\Model\Bag\Context::ARRAY_ACCESS: $out .= 'index(' . "\n" . $value->accept($this, $handle, $eldnah) . "\n" . $_ . ' )'; break; case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS: $out .= 'attribute(\'' . $value . '\')'; break; case Ruler\Model\Bag\Context::METHOD_ACCESS: $out .= 'call(' . "\n" . $value->accept($this, $handle, $eldnah) . "\n" . $_ . ' )'; break; } } $this->_indentation -= 2; } return $out; }
/** * Visit an element. * * @param \Hoa\Visitor\Element $element Element to visit. * @param mixed &$handle Handle (reference). * @param mixed $eldnah Handle (not reference). * @return mixed */ public function visit(Visitor\Element $element, &$handle = null, $eldnah = null) { $out = null; if ($element instanceof Ruler\Model) { $out .= $element->getExpression()->accept($this, $handle, $eldnah); } elseif ($element instanceof Ruler\Model\Operator) { $name = $element->getName(); $arguments = []; foreach ($element->getArguments() as $argument) { $arguments[] = $argument->accept($this, $handle, $eldnah); } if (true === $element->isFunction()) { $out .= $name . '(' . implode(', ', $arguments) . ')'; } else { if (!isset($arguments[1])) { $_out = $name . ' ' . $arguments[0]; } else { $_out = $arguments[0] . ' ' . $name . ' ' . $arguments[1]; } if (false === Ruler\Model\Operator::isToken($name)) { $_out = '(' . $_out . ')'; } $out .= $_out; } } elseif ($element instanceof Ruler\Model\Bag\Scalar) { $value = $element->getValue(); if (true === $value) { $out .= 'true'; } elseif (false === $value) { $out .= 'false'; } elseif (null === $value) { $out .= 'null'; } elseif (is_numeric($value)) { $out .= (string) $value; } else { $out .= '\'' . str_replace('\\', '\\\'', $value) . '\''; } } elseif ($element instanceof Ruler\Model\Bag\RulerArray) { $values = []; foreach ($element->getArray() as $value) { $values[] = $value->accept($this, $handle, $eldnah); } $out .= '[' . implode(', ', $values) . ']'; } elseif ($element instanceof Ruler\Model\Bag\Context) { $out .= $element->getId(); foreach ($element->getDimensions() as $dimension) { $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE]; switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) { case Ruler\Model\Bag\Context::ARRAY_ACCESS: $out .= '[' . $value->accept($this, $handle, $eldnah) . ']'; break; case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS: $out .= '.' . $value; break; case Ruler\Model\Bag\Context::METHOD_ACCESS: $out .= '.' . $value->accept($this, $handle, $eldnah); break; } } } return $out; }