Hoa central
Disassembly.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Ruler\Visitor;
38 
39 use Hoa\Ruler;
40 use Hoa\Visitor;
41 
50 class Disassembly implements Visitor\Visit
51 {
60  public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
61  {
62  $out = null;
63 
64  if ($element instanceof Ruler\Model) {
65  $out .= $element->getExpression()->accept($this, $handle, $eldnah);
66  } elseif ($element instanceof Ruler\Model\Operator) {
67  $name = $element->getName();
68  $arguments = [];
69 
70  foreach ($element->getArguments() as $argument) {
71  $arguments[] = $argument->accept($this, $handle, $eldnah);
72  }
73 
74  if (true === $element->isFunction()) {
75  $out .= $name . '(' . implode(', ', $arguments) . ')';
76  } else {
77  if (!isset($arguments[1])) {
78  $_out = $name . ' ' . $arguments[0];
79  } else {
80  $_out = $arguments[0] . ' ' . $name . ' ' . $arguments[1];
81  }
82 
83  if (false === Ruler\Model\Operator::isToken($name)) {
84  $_out = '(' . $_out . ')';
85  }
86 
87  $out .= $_out;
88  }
89  } elseif ($element instanceof Ruler\Model\Bag\Scalar) {
90  $value = $element->getValue();
91 
92  if (true === $value) {
93  $out .= 'true';
94  } elseif (false === $value) {
95  $out .= 'false';
96  } elseif (null === $value) {
97  $out .= 'null';
98  } elseif (is_numeric($value)) {
99  $out .= (string) $value;
100  } else {
101  $out .= '\'' . str_replace('\\', '\\\'', $value) . '\'';
102  }
103  } elseif ($element instanceof Ruler\Model\Bag\RulerArray) {
104  $values = [];
105 
106  foreach ($element->getArray() as $value) {
107  $values[] = $value->accept($this, $handle, $eldnah);
108  }
109 
110  $out .= '[' . implode(', ', $values) . ']';
111  } elseif ($element instanceof Ruler\Model\Bag\Context) {
112  $out .= $element->getId();
113 
114  foreach ($element->getDimensions() as $dimension) {
115  $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
116 
117  switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) {
118  case Ruler\Model\Bag\Context::ARRAY_ACCESS:
119  $out .=
120  '[' .
121  $value->accept($this, $handle, $eldnah) .
122  ']';
123 
124  break;
125 
126  case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS:
127  $out .= '.' . $value;
128 
129  break;
130 
131  case Ruler\Model\Bag\Context::METHOD_ACCESS:
132  $out .= '.' . $value->accept($this, $handle, $eldnah);
133 
134  break;
135  }
136  }
137  }
138 
139  return $out;
140  }
141 }
visit(Visitor\Element $element, &$handle=null, $eldnah=null)
Definition: Disassembly.php:60