Hoa central
Declaration.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Praspel\Model;
38 
39 use Hoa\Iterator;
40 use Hoa\Praspel;
41 use Hoa\Realdom;
42 
51 abstract class Declaration
52  extends Clause
53  implements Iterator\Aggregate,
54  \ArrayAccess,
55  \Countable
56 {
62  protected $_variables = [];
63 
69  protected $_predicates = [];
70 
76  protected $_let = false;
77 
78 
79 
86  public function offsetExists($offset)
87  {
88  return isset($this->_variables[$offset]);
89  }
90 
97  public function offsetGet($offset)
98  {
99  return $this->getVariable($offset);
100  }
101 
109  public function getVariable($name, $borrowing = false)
110  {
111  if (true === $borrowing) {
112  $out = new Variable\Borrowing($name, $this->_let, $this);
113  $this->_let = false;
114 
115  return $out;
116  }
117 
118  if ('\old(' === substr($name, 0, 5)) {
119  $variable = $this->getVariable($name, true);
120 
121  return new Realdom\Crate\Constant(
122  $variable->getBorrowedVariable(),
123  function () use ($variable) {
124  return $variable->getName();
125  },
126  $this
127  );
128  }
129 
130  if (false === $this->offsetExists($name)) {
131  $variable = new Variable($name, $this->_let, $this);
132  $this->_let = false;
133 
134  return $this->_variables[$name] = $variable;
135  }
136 
137  return $this->_variables[$name];
138  }
139 
147  public function addVariable($name, Variable $variable)
148  {
149  return $this->_variables[$name] = $variable;
150  }
151 
159  public function offsetSet($offset, $value)
160  {
161  $variable = $this->offsetGet($offset);
162  $old = $variable->getValue();
163  $variable->setValue($value);
164 
165  return $old;
166  }
167 
174  public function offsetUnset($offset)
175  {
176  unset($this->_variables[$offset]);
177 
178  return;
179  }
180 
188  public function __get($name)
189  {
190  if ('let' !== $name) {
191  return $this->$name;
192  }
193 
194  $this->_let = true;
195 
196  return $this;
197  }
198 
204  public function getIterator()
205  {
206  return new Iterator\CallbackFilter(
207  new Iterator\Map($this->getLocalVariables()),
208  function (Variable $variable) {
209  return false === $variable->isLocal();
210  }
211  );
212  }
213 
219  public function count()
220  {
221  return count($this->_variables);
222  }
223 
229  public function &getLocalVariables()
230  {
231  return $this->_variables;
232  }
233 
239  public function getInScopeVariables()
240  {
241  $out = [];
242  $clause = $this->getName();
243  $current = $this;
244 
245  while (null !== $current = $current->getParent()) {
246  if (false === $current->clauseExists($clause)) {
247  continue;
248  }
249 
250  $localVariables = &$current->getClause($clause)->getLocalVariables();
251 
252  foreach ($localVariables as $name => &$variables) {
253  $out[$name] = &$variables;
254  }
255  }
256 
257  return $out;
258  }
259 
266  public function predicate($predicate)
267  {
268  $this->_predicates[] = $predicate;
269 
270  return $this;
271  }
272 
278  public function getPredicates()
279  {
280  return $this->_predicates;
281  }
282 }
getVariable($name, $borrowing=false)
addVariable($name, Variable $variable)