_variables[$offset]); } /** * Get or create a variable. * * @param string $offset Variable name. * @return \Hoa\Praspel\Model\Variable */ public function offsetGet($offset) { return $this->getVariable($offset); } /** * Declare or get a new variable. * * @param string $name Variable name. * @param bool $borrowing Borrowing variable or not. * @return mixed */ public function getVariable($name, $borrowing = false) { if (true === $borrowing) { $out = new Variable\Borrowing($name, $this->_let, $this); $this->_let = false; return $out; } if ('\old(' === substr($name, 0, 5)) { $variable = $this->getVariable($name, true); return new Realdom\Crate\Constant( $variable->getBorrowedVariable(), function () use ($variable) { return $variable->getName(); }, $this ); } if (false === $this->offsetExists($name)) { $variable = new Variable($name, $this->_let, $this); $this->_let = false; return $this->_variables[$name] = $variable; } return $this->_variables[$name]; } /** * Add a variable. * * @param string $name Name. * @param \Hoa\Praspel\Model\Variable $variable Variable. * @return \Hoa\Praspel\Model\Variable */ public function addVariable($name, Variable $variable) { return $this->_variables[$name] = $variable; } /** * Set a value to a variable. * * @param string $offset Variable name. * @param mixed $value Variable value. * @return mixed */ public function offsetSet($offset, $value) { $variable = $this->offsetGet($offset); $old = $variable->getValue(); $variable->setValue($value); return $old; } /** * Delete a variable. * * @param string $offset Variable name. * @return void */ public function offsetUnset($offset) { unset($this->_variables[$offset]); return; } /** * Allow to write $clause->let['var'] = … to define a local variable (if * $name is not equal to "let", then it is a normal behavior). * * @param string $name Name. * @return \Hoa\Praspel\Model\Declaration */ public function __get($name) { if ('let' !== $name) { return $this->$name; } $this->_let = true; return $this; } /** * Iterator over local variables. * * @return \Hoa\Iterator\CallbackFilter */ public function getIterator() { return new Iterator\CallbackFilter( new Iterator\Map($this->getLocalVariables()), function (Variable $variable) { return false === $variable->isLocal(); } ); } /** * Count number of variables. * * @return int */ public function count() { return count($this->_variables); } /** * Get local variables. * * @return array */ public function &getLocalVariables() { return $this->_variables; } /** * Get in-scope variables. * * @return array */ public function getInScopeVariables() { $out = []; $clause = $this->getName(); $current = $this; while (null !== $current = $current->getParent()) { if (false === $current->clauseExists($clause)) { continue; } $localVariables = &$current->getClause($clause)->getLocalVariables(); foreach ($localVariables as $name => &$variables) { $out[$name] = &$variables; } } return $out; } /** * Add a predicate. * * @param string $predicate Predicate. * @return \Hoa\Praspel\Model\Declaration */ public function predicate($predicate) { $this->_predicates[] = $predicate; return $this; } /** * Get all predicates. * * @return array */ public function getPredicates() { return $this->_predicates; } }