You are here

public function BusinessRulesProcessor::evaluateVariable in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Util/BusinessRulesProcessor.php \Drupal\business_rules\Util\BusinessRulesProcessor::evaluateVariable()

Evaluate the variable value.

Parameters

\Drupal\business_rules\Entity\Variable $variable: The variable.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event.

Return value

\Drupal\business_rules\VariableObject|\Drupal\business_rules\VariablesSet The evaluated variable or a VariableSet which processed variables.

Throws

\Exception

1 call to BusinessRulesProcessor::evaluateVariable()
BusinessRulesProcessor::evaluateVariables in src/Util/BusinessRulesProcessor.php
Evaluate all variables from a VariableSet for a given event.

File

src/Util/BusinessRulesProcessor.php, line 698

Class

BusinessRulesProcessor
Class BusinessRulesProcessor.

Namespace

Drupal\business_rules\Util

Code

public function evaluateVariable(Variable $variable, BusinessRulesEvent $event) {

  // Do note evaluate the same variable twice to avid overload.
  if (array_key_exists($variable
    ->id(), $this->evaluatedVariables)) {
    return NULL;
  }

  /** @var \Drupal\business_rules\VariablesSet $eventVariables */

  /** @var \Drupal\business_rules\VariableObject $item */
  $eventVariables = $event
    ->getArgument('variables');
  $variable_variables = $variable
    ->getVariables();
  $this
    ->evaluateVariables($variable_variables, $event);
  $value = $variable
    ->evaluate($event);
  if ($value instanceof VariableObject) {
    $this->evaluatedVariables[$variable
      ->id()] = $variable
      ->id();
    $eventVariables
      ->append($value);
    $this->debugArray['variables'][$this->ruleBeingExecuted
      ->id()][$variable
      ->id()] = $value;
    return $value;
  }
  elseif ($value instanceof VariablesSet) {
    if ($value
      ->count()) {
      foreach ($value
        ->getVariables() as $item) {
        $this->evaluatedVariables[$item
          ->getId()] = $item
          ->getId();
        $eventVariables
          ->append($item);
        $this->debugArray['variables'][$this->ruleBeingExecuted
          ->id()][$item
          ->getId()] = $item;
      }
    }
    return $value;
  }
  else {
    throw new \Exception(get_class($value) . '::evaluate should return instance of ' . get_class(new VariableObject()) . ' or ' . get_class(new VariablesSet()) . '.');
  }
}