You are here

class VariablesSet in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/VariablesSet.php \Drupal\business_rules\VariablesSet

Class VariablesSet to be returned on each BusinessRulesVariable plugin.

@package Drupal\business_rules

Hierarchy

Expanded class hierarchy of VariablesSet

16 files declare their use of VariablesSet
BusinessRulesConditionPluginInterface.php in src/Plugin/BusinessRulesConditionPluginInterface.php
BusinessRulesItemPluginBase.php in src/Plugin/BusinessRulesItemPluginBase.php
BusinessRulesItemPluginInterface.php in src/Plugin/BusinessRulesItemPluginInterface.php
BusinessRulesProcessor.php in src/Util/BusinessRulesProcessor.php
DataComparison.php in src/Plugin/BusinessRulesCondition/DataComparison.php

... See full list

File

src/VariablesSet.php, line 10

Namespace

Drupal\business_rules
View source
class VariablesSet {

  /**
   * The variables storage.
   *
   * @var array
   */
  protected $variables = [];

  /**
   * Append the variable to the array.
   *
   * @param \Drupal\business_rules\VariableObject $variable
   *   The variable set.
   */
  public function append(VariableObject $variable) {
    $this->variables[$variable
      ->getId()] = $variable;
  }

  /**
   * Replace the content of one variable.
   *
   * @param string $variable_id
   *   The variable id.
   * @param mixed $value
   *   The variable value.
   *
   * @internal param \Drupal\business_rules\VariableObject $variable
   */
  public function replaceValue($variable_id, $value) {
    if ($this
      ->count()) {
      foreach ($this->variables as $key => $var) {
        if ($var
          ->getId() == $variable_id) {
          $this->variables[$key]
            ->setValue($value);
        }
      }
    }
  }

  /**
   * Number of variables inside the set.
   *
   * @return int
   *   The number of variables on the variable set.
   */
  public function count() {
    return count($this->variables);
  }

  /**
   * Remove one variable from the variables set.
   *
   * @param string $variable_id
   *   The variable id.
   */
  public function remove($variable_id) {
    if ($this
      ->count()) {
      foreach ($this->variables as $key => $variable) {
        if ($variable
          ->getId() == $variable_id) {
          unset($this->variables[$key]);
        }
      }
    }
  }

  /**
   * Return all variables.
   *
   * @return array
   *   Array of variables.
   */
  public function getVariables() {
    return $this->variables;
  }

  /**
   * Return variable by id.
   *
   * @param string $variable_id
   *   The variable id.
   *
   * @return VariableObject|null
   *   The variable object or null if variable id not exists.
   */
  public function getVariable($variable_id) {
    if ($this
      ->count()) {
      foreach ($this->variables as $variable) {
        if ($variable
          ->getId() == $variable_id) {
          return $variable;
        }
      }
    }
    return NULL;
  }

  /**
   * Return the variables ids.
   *
   * @return array
   *   Array of variables ids.
   */
  public function getVariablesIds() {
    $ids = [];
    if ($this
      ->count()) {
      foreach ($this->variables as $variable) {
        $ids[] = $variable
          ->getId();
      }
    }
    return $ids;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VariablesSet::$variables protected property The variables storage.
VariablesSet::append public function Append the variable to the array.
VariablesSet::count public function Number of variables inside the set.
VariablesSet::getVariable public function Return variable by id.
VariablesSet::getVariables public function Return all variables.
VariablesSet::getVariablesIds public function Return the variables ids.
VariablesSet::remove public function Remove one variable from the variables set.
VariablesSet::replaceValue public function Replace the content of one variable.