Hoa central
Operator.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Ruler\Model;
38 
39 use Hoa\Visitor;
40 
49 class Operator implements Visitor\Element
50 {
56  const LAZY_BREAK = false;
57 
63  const LAZY_CONTINUE = true;
64 
70  protected $_name = null;
71 
77  protected $_arguments = null;
78 
84  protected $_function = true;
85 
91  protected $_laziness = false;
92 
101  public function __construct(
102  $name,
103  Array $arguments = [],
104  $isFunction = true
105  ) {
106  $this->setName($name);
107  $this->setLaziness('and' === $name || 'or' === $name);
108  $this->setArguments($arguments);
109  $this->setFunction($isFunction);
110 
111  return;
112  }
113 
120  protected function setName($name)
121  {
122  $old = $this->_name;
123  $this->_name = $name;
124 
125  return $old;
126  }
127 
133  public function getName()
134  {
135  return $this->_name;
136  }
137 
144  protected function setArguments(Array $arguments)
145  {
146  foreach ($arguments as &$argument) {
147  if (is_scalar($argument) || null === $argument) {
148  $argument = new Bag\Scalar($argument);
149  } elseif (is_array($argument)) {
150  $argument = new Bag\RulerArray($argument);
151  }
152  }
153 
154  $old = $this->_arguments;
155  $this->_arguments = $arguments;
156 
157  return $old;
158  }
159 
165  public function getArguments()
166  {
167  return $this->_arguments;
168  }
169 
176  protected function setFunction($isFunction)
177  {
178  $old = $this->_function;
179  $this->_function = $isFunction;
180 
181  return $old;
182  }
183 
189  public function isFunction()
190  {
191  return $this->_function;
192  }
193 
200  protected function setLaziness($isLazy)
201  {
202  $old = $this->_laziness;
203  $this->_laziness = $isLazy;
204 
205  return $old;
206  }
207 
213  public function isLazy()
214  {
215  return $this->_laziness;
216  }
217 
224  public function shouldBreakLazyEvaluation($value)
225  {
226  switch ($this->_name) {
227  case 'and':
228  if (false === $value) {
229  return self::LAZY_BREAK;
230  }
231 
232  break;
233 
234  case 'or':
235  if (true === $value) {
236  return self::LAZY_BREAK;
237  }
238 
239  break;
240  }
241 
242  return self::LAZY_CONTINUE;
243  }
244 
251  public static function isToken($operator)
252  {
253  static $_tokens = ['not', 'and', 'or', 'xor'];
254 
255  return true === in_array($operator, $_tokens);
256  }
257 
266  public function accept(
267  Visitor\Visit $visitor,
268  &$handle = null,
269  $eldnah = null
270  ) {
271  return $visitor->visit($this, $handle, $eldnah);
272  }
273 }
setFunction($isFunction)
Definition: Operator.php:176
__construct($name, Array $arguments=[], $isFunction=true)
Definition: Operator.php:101
accept(Visitor\Visit $visitor, &$handle=null, $eldnah=null)
Definition: Operator.php:266
static isToken($operator)
Definition: Operator.php:251
shouldBreakLazyEvaluation($value)
Definition: Operator.php:224
setArguments(Array $arguments)
Definition: Operator.php:144