Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Ruler\Visitor\Asserter Class Reference
Inheritance diagram for Hoa\Ruler\Visitor\Asserter:

Public Member Functions

 __construct (Ruler\Context $context=null)
 
 visit (Visitor\Element $element, &$handle=null, $eldnah=null)
 
 visitModel (Ruler\Model $element, &$handle=null, $eldnah=null)
 
 setContext (Ruler\Context $context)
 
 getContext ()
 
 setOperator ($operator, $callable)
 
 operatorExists ($operator)
 
 getOperator ($operator)
 
 getOperators ()
 
- Public Member Functions inherited from Hoa\Visitor\Visit
 visit (Element $element, &$handle=null, $eldnah=null)
 

Protected Member Functions

 visitOperator (Ruler\Model\Operator $element, &$handle=null, $eldnah=null)
 
 visitScalar (Ruler\Model\Bag\Scalar $element, &$handle=null, $eldnah=null)
 
 visitArray (Ruler\Model\Bag\RulerArray $element, &$handle=null, $eldnah=null)
 
 visitContext (Ruler\Model\Bag\Context $element, &$handle=null, $eldnah=null)
 
 visitContextArray (&$contextPointer, Array $dimension, $dimensionNumber, $elementId, &$handle=null, $eldnah=null)
 
 visitContextAttribute (&$contextPointer, Array $dimension, $dimensionNumber, $elementId, &$handle=null, $eldnah=null)
 
 visitContextMethod (&$contextPointer, Array $dimension, $dimensionNumber, $elementId, &$handle=null, $eldnah=null)
 

Protected Attributes

 $_context = null
 
 $_operators = []
 

Detailed Description

Class .

Asserter: evaluate a model representing a rule.

Definition at line 51 of file Asserter.php.

Constructor & Destructor Documentation

Hoa\Ruler\Visitor\Asserter::__construct ( Ruler\Context  $context = null)

Constructor.

Parameters
\Hoa\Ruler\Context$contextContext.
Returns
void

Definition at line 75 of file Asserter.php.

76  {
77  if (null !== $context) {
78  $this->setContext($context);
79  }
80 
81  $this->setOperator('and', function ($a = false, $b = false) { return $a && $b; });
82  $this->setOperator('or', function ($a = false, $b = false) { return $a || $b; });
83  $this->setOperator('xor', function ($a, $b) { return (bool) ($a ^ $b); });
84  $this->setOperator('not', function ($a) { return !$a; });
85  $this->setOperator('=', function ($a, $b) { return $a == $b; });
86  $this->setOperator('is', $this->getOperator('='));
87  $this->setOperator('!=', function ($a, $b) { return $a != $b; });
88  $this->setOperator('>', function ($a, $b) { return $a > $b; });
89  $this->setOperator('>=', function ($a, $b) { return $a >= $b; });
90  $this->setOperator('<', function ($a, $b) { return $a < $b; });
91  $this->setOperator('<=', function ($a, $b) { return $a <= $b; });
92  $this->setOperator('in', function ($a, Array $b) { return in_array($a, $b); });
93  $this->setOperator('sum', function () { return array_sum(func_get_args()); });
94 
95  return;
96  }
setOperator($operator, $callable)
Definition: Asserter.php:462
setContext(Ruler\Context $context)
Definition: Asserter.php:437

Here is the call graph for this function:

Member Function Documentation

Hoa\Ruler\Visitor\Asserter::getContext ( )

Get context.

Returns

Definition at line 450 of file Asserter.php.

451  {
452  return $this->_context;
453  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::getOperator (   $operator)

Get an operator.

Parameters
string$operatorOperator.
Returns
string

Definition at line 486 of file Asserter.php.

487  {
488  if (false === $this->operatorExists($operator)) {
489  return null;
490  }
491 
492  $handle = &$this->_operators[$operator];
493 
494  if (!($handle instanceof Core\Consistency\Xcallable)) {
495  $handle = xcallable($handle);
496  }
497 
498  return $this->_operators[$operator];
499  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::getOperators ( )

Get all operators.

Returns
array

Definition at line 506 of file Asserter.php.

507  {
508  foreach ($this->_operators as &$operator) {
509  if (!($operator instanceof Core\Consistency\Xcallable)) {
510  $operator = xcallable($operator);
511  }
512  }
513 
514  return $this->_operators;
515  }
Hoa\Ruler\Visitor\Asserter::operatorExists (   $operator)

Check if an operator exists.

Parameters
string$operatorOperator.
Returns
bool

Definition at line 475 of file Asserter.php.

476  {
477  return true === array_key_exists($operator, $this->_operators);
478  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::setContext ( Ruler\Context  $context)

Set context.

Parameters
\Hoa\Ruler\Context$contextContext.
Returns

Definition at line 437 of file Asserter.php.

438  {
439  $old = $this->_context;
440  $this->_context = $context;
441 
442  return $old;
443  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::setOperator (   $operator,
  $callable 
)

Set an operator.

Parameters
string$operatorOperator.
callable$callableCallable.
Returns
Ruler

Definition at line 462 of file Asserter.php.

463  {
464  $this->_operators[$operator] = $callable;
465 
466  return $this;
467  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visit ( Visitor\Element  $element,
$handle = null,
  $eldnah = null 
)

Visit an element.

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed
Exceptions

Definition at line 107 of file Asserter.php.

108  {
109  if ($element instanceof Ruler\Model) {
110  return $this->visitModel($element, $handle, $eldnah);
111  }
112 
113  if ($element instanceof Ruler\Model\Operator) {
114  return $this->visitOperator($element, $handle, $eldnah);
115  }
116 
117  if ($element instanceof Ruler\Model\Bag\Scalar) {
118  return $this->visitScalar($element, $handle, $eldnah);
119  }
120 
121  if ($element instanceof Ruler\Model\Bag\RulerArray) {
122  return $this->visitArray($element, $handle, $eldnah);
123  }
124 
125  if ($element instanceof Ruler\Model\Bag\Context) {
126  return $this->visitContext($element, $handle, $eldnah);
127  }
128  }
visitContext(Ruler\Model\Bag\Context $element, &$handle=null, $eldnah=null)
Definition: Asserter.php:216
visitOperator(Ruler\Model\Operator $element, &$handle=null, $eldnah=null)
Definition: Asserter.php:151
visitModel(Ruler\Model $element, &$handle=null, $eldnah=null)
Definition: Asserter.php:138
visitScalar(Ruler\Model\Bag\Scalar $element, &$handle=null, $eldnah=null)
Definition: Asserter.php:184
visitArray(Ruler\Model\Bag\RulerArray $element, &$handle=null, $eldnah=null)
Definition: Asserter.php:197

Here is the call graph for this function:

Hoa\Ruler\Visitor\Asserter::visitArray ( Ruler\Model\Bag\RulerArray  $element,
$handle = null,
  $eldnah = null 
)
protected

Visit an array

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
array

Definition at line 197 of file Asserter.php.

198  {
199  $out = [];
200 
201  foreach ($element->getArray() as $key => $data) {
202  $out[$key] = $data->accept($this, $handle, $eldnah);
203  }
204 
205  return $out;
206  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitContext ( Ruler\Model\Bag\Context  $element,
$handle = null,
  $eldnah = null 
)
protected

Visit a context

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed

Definition at line 216 of file Asserter.php.

217  {
218  $context = $this->getContext();
219 
220  if (null === $context) {
221  throw new Ruler\Exeption\Asserter(
222  'Assert needs a context to work properly.',
223  1
224  );
225  }
226 
227  $id = $element->getId();
228 
229  if (!isset($context[$id])) {
230  throw new Ruler\Exception\Asserter(
231  'Context reference %s does not exists.',
232  2,
233  $id
234  );
235  }
236 
237  $contextPointer = $context[$id];
238 
239  foreach ($element->getDimensions() as $dimensionNumber => $dimension) {
240  ++$dimensionNumber;
241 
242  switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) {
243  case Ruler\Model\Bag\Context::ARRAY_ACCESS:
244  $this->visitContextArray(
245  $contextPointer,
246  $dimension,
247  $dimensionNumber,
248  $id,
249  $handle,
250  $eldnah
251  );
252 
253  break;
254 
255  case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS:
256  $this->visitContextAttribute(
257  $contextPointer,
258  $dimension,
259  $dimensionNumber,
260  $id,
261  $handle,
262  $eldnah
263  );
264 
265  break;
266 
267  case Ruler\Model\Bag\Context::METHOD_ACCESS:
268  $this->visitContextMethod(
269  $contextPointer,
270  $dimension,
271  $dimensionNumber,
272  $id,
273  $handle,
274  $eldnah
275  );
276 
277  break;
278  }
279  }
280 
281  return $contextPointer;
282  }
visitContextArray(&$contextPointer, Array $dimension, $dimensionNumber, $elementId, &$handle=null, $eldnah=null)
Definition: Asserter.php:295
visitContextMethod(&$contextPointer, Array $dimension, $dimensionNumber, $elementId, &$handle=null, $eldnah=null)
Definition: Asserter.php:387
visitContextAttribute(&$contextPointer, Array $dimension, $dimensionNumber, $elementId, &$handle=null, $eldnah=null)
Definition: Asserter.php:342

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitContextArray ( $contextPointer,
Array  $dimension,
  $dimensionNumber,
  $elementId,
$handle = null,
  $eldnah = null 
)
protected

Visit a context array

Parameters
mixed&$contextPointerPointer to the current context.
array$dimensionDimension bucket.
int$dimensionNumberDimension number.
string$elementIdElement name.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
void

Definition at line 295 of file Asserter.php.

302  {
303  $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
304  $key = $value->accept($this, $handle, $eldnah);
305 
306  if (!is_array($contextPointer)) {
307  throw new Ruler\Exception\Asserter(
308  'Try to access to an undefined index: %s ' .
309  '(dimension number %d of %s), because it is ' .
310  'not an array.',
311  3,
312  [$key, $dimensionNumber, $elementId]
313  );
314  }
315 
316  if (!isset($contextPointer[$key])) {
317  throw new Ruler\Exception\Asserter(
318  'Try to access to an undefined index: %s ' .
319  '(dimension number %d of %s).',
320  4,
321  [$key, $dimensionNumber, $elementId]
322  );
323  }
324 
325  $contextPointer = $contextPointer[$key];
326 
327  return;
328  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitContextAttribute ( $contextPointer,
Array  $dimension,
  $dimensionNumber,
  $elementId,
$handle = null,
  $eldnah = null 
)
protected

Visit a context attribute

Parameters
mixed&$contextPointerPointer to the current context.
array$dimensionDimension bucket.
int$dimensionNumberDimension number.
string$elementIdElement name.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
void

Definition at line 342 of file Asserter.php.

349  {
350  $attribute = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
351 
352  if (!is_object($contextPointer)) {
353  throw new Ruler\Exception\Asserter(
354  'Try to read an undefined attribute: %s ' .
355  '(dimension number %d of %s), because it is ' .
356  'not an object.',
357  5,
358  [$attribute, $dimensionNumber, $elementId]
359  );
360  }
361 
362  if (!property_exists($contextPointer, $attribute)) {
363  throw new Ruler\Exception\Asserter(
364  'Try to read an undefined attribute: %s ' .
365  '(dimension number %d of %s).',
366  6,
367  [$attribute, $dimensionNumber, $elementId]
368  );
369  }
370 
371  $contextPointer = $contextPointer->$attribute;
372 
373  return;
374  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitContextMethod ( $contextPointer,
Array  $dimension,
  $dimensionNumber,
  $elementId,
$handle = null,
  $eldnah = null 
)
protected

Visit a context method

Parameters
mixed&$contextPointerPointer to the current context.
array$dimensionDimension bucket.
int$dimensionNumberDimension number.
string$elementIdElement name.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
void

Definition at line 387 of file Asserter.php.

394  {
395  $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
396  $method = $value->getName();
397 
398  if (!is_object($contextPointer)) {
399  throw new Ruler\Exception\Asserter(
400  'Try to call an undefined method: %s ' .
401  '(dimension number %d of %s), because it is ' .
402  'not an object.',
403  7,
404  [$method, $dimensionNumber, $elementId]
405  );
406  }
407 
408  if (!method_exists($contextPointer, $method)) {
409  throw new Ruler\Exception\Asserter(
410  'Try to call an undefined method: %s ' .
411  '(dimension number %d of %s).',
412  8,
413  [$method, $dimensionNumber, $elementId]
414  );
415  }
416 
417  $arguments = [];
418 
419  foreach ($value->getArguments() as $argument) {
420  $arguments[] = $argument->accept($this, $handle, $eldnah);
421  }
422 
423  $contextPointer = call_user_func_array(
424  [$contextPointer, $method],
425  $arguments
426  );
427 
428  return;
429  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitModel ( Ruler\Model  $element,
$handle = null,
  $eldnah = null 
)

Visit a model

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed

Definition at line 138 of file Asserter.php.

139  {
140  return (bool) $element->getExpression()->accept($this, $handle, $eldnah);
141  }

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitOperator ( Ruler\Model\Operator  $element,
$handle = null,
  $eldnah = null 
)
protected

Visit an operator

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed

Definition at line 151 of file Asserter.php.

152  {
153  $name = $element->getName();
154  $arguments = [];
155 
156  foreach ($element->getArguments() as $argument) {
157  $value = $argument->accept($this, $handle, $eldnah);
158  $arguments[] = $value;
159 
160  if ($element::LAZY_BREAK === $element->shouldBreakLazyEvaluation($value)) {
161  break;
162  }
163  }
164 
165  if (false === $this->operatorExists($name)) {
166  throw new Ruler\Exception\Asserter(
167  'Operator %s does not exist.',
168  0,
169  $name
170  );
171  }
172 
173  return $this->getOperator($name)->distributeArguments($arguments);
174  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Ruler\Visitor\Asserter::visitScalar ( Ruler\Model\Bag\Scalar  $element,
$handle = null,
  $eldnah = null 
)
protected

Visit a scalar

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed

Definition at line 184 of file Asserter.php.

185  {
186  return $element->getValue();
187  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Ruler\Visitor\Asserter::$_context = null
protected

Definition at line 58 of file Asserter.php.

Hoa\Ruler\Visitor\Asserter::$_operators = []
protected

Definition at line 65 of file Asserter.php.


The documentation for this class was generated from the following file: