You are here

class RulesConditionalCase in Conditional Rules 8

Same name and namespace in other branches
  1. 7 includes/rules_conditional.plugin.inc \RulesConditionalCase

Switch case.

Hierarchy

Expanded class hierarchy of RulesConditionalCase

1 string reference to 'RulesConditionalCase'
rules_conditional_rules_plugin_info in ./rules_conditional.rules.inc
Implements hook_rules_plugin_info().

File

includes/rules_conditional.plugin.inc, line 240
Rules plugin implementation.

View source
class RulesConditionalCase extends RulesConditionalElement {
  protected $itemName = 'case';

  /**
   * @var RulesPlugin
   */
  protected $condition;

  /**
   * Evaluated condition results.
   * @var array
   */
  protected $conditionResultCache = array();
  public function __construct($settings = array(), $fallThrough = FALSE) {
    parent::__construct();
    if (isset($settings['value'])) {
      $this->settings['value'] = $settings['value'];
    }
    elseif (isset($settings['value:select'])) {
      $this->settings['value:select'] = $settings['value:select'];
    }
    $this->settings += array(
      'fall_through' => $fallThrough,
    );
  }
  public function __destruct() {
    unset($this->condition);
    unset($this->conditionResultCache);
  }
  public function forceSetUp() {
    parent::forceSetUp();
    $this
      ->setUpCondition();
  }
  public function pluginParameterInfo() {
    $parameterInfo = array(
      'value' => array(
        'type' => '*',
        'label' => t('Data value'),
        'description' => t('The value to compare the data with.'),
        'allow null' => TRUE,
      ),
      'fall_through' => array(
        'type' => 'boolean',
        'label' => t('Fall through'),
        'description' => t('Fall through to next case when complete. If this option is checked, the next case is automatically executed (regardless of the case value) when this case is finished. If not, the switch will terminate when the case is finished.'),
        'optional' => TRUE,
        'default value' => FALSE,
        'restriction' => 'input',
      ),
    );

    // Derive parameter info from switch variable selector.
    $dataSelector = isset($this->parent->settings['data:select']) ? $this->parent->settings['data:select'] : NULL;
    if ($wrapper = $this
      ->applyDataSelector($dataSelector)) {
      $parameterInfo['value']['type'] = $wrapper
        ->type();
    }
    return $parameterInfo;
  }
  public function stateVariables($element = NULL) {
    $this
      ->forceSetUp();
    if (!isset($element) || $element === $this->condition) {
      return parent::stateVariables();
    }
    else {

      // Add assertions from the condition.
      $variables = parent::stateVariables($element);
      if (isset($this->condition) && ($assertions = $this->condition
        ->call('variableInfoAssertions'))) {
        $variables = RulesData::addMetadataAssertions($variables, $assertions);
      }
      return $variables;
    }
  }
  protected function setUpCondition() {
    if (!isset($this->condition) && isset($this->parent)) {

      // Prepare settings for 'data_is' condition.
      $settings = array(
        'data:select' => $this->parent->settings['data:select'],
        'op' => '==',
      );
      if (isset($this->settings['value:select'])) {
        $settings['value:select'] = $this->settings['value:select'];
      }
      elseif (isset($this->settings['value'])) {
        $settings['value'] = $this->settings['value'];
      }
      else {

        // Abort if settings are incomplete.
        return;
      }

      // Set up 'data_is'.
      $this->condition = rules_condition('data_is', $settings);
      $this->condition->parent = $this;
      $this->condition
        ->processSettings();
    }
  }

  /**
   * Returns whether this case should fall through.
   */
  public function fallThrough() {
    return !empty($this->settings['fall_through']);
  }

  /**
   * Determines whether this branch can be evaluated.
   */
  public function canEvaluate(RulesState $state) {
    $this
      ->forceSetUp();

    // Check if this element has fallen through.
    if ($previous = $this
      ->getPreviousSibling()) {

      /** @var $previous self */
      if ($previous instanceof self && $previous
        ->fallThrough() && $previous
        ->canEvaluate($state)) {
        return TRUE;
      }
    }

    // Evaluate condition for the given state once.
    $this->conditionResultCache += array(
      'state' => array(),
      'result' => array(),
    );
    if (empty($this->conditionResultCache['state']) || !($cacheKey = array_search($state, $this->conditionResultCache['state'], TRUE))) {
      $cacheKey = count($this->conditionResultCache['state']);
      $this->conditionResultCache['state'][$cacheKey] = $state;
      $this->conditionResultCache['result'][$cacheKey] = $this->condition
        ->evaluate($state);
    }
    return !empty($this->conditionResultCache['result'][$cacheKey]);
  }
  public function resetInternalCache() {
    parent::resetInternalCache();
    $this->condition = NULL;
    $this->conditionResultCache = array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesConditionalCase::$condition protected property
RulesConditionalCase::$conditionResultCache protected property Evaluated condition results.
RulesConditionalCase::$itemName protected property
RulesConditionalCase::canEvaluate public function Determines whether this branch can be evaluated. Overrides RulesConditionalElement::canEvaluate
RulesConditionalCase::fallThrough public function Returns whether this case should fall through.
RulesConditionalCase::forceSetUp public function
RulesConditionalCase::pluginParameterInfo public function
RulesConditionalCase::resetInternalCache public function
RulesConditionalCase::setUpCondition protected function
RulesConditionalCase::stateVariables public function
RulesConditionalCase::__construct public function
RulesConditionalCase::__destruct public function
RulesConditionalElement::$parent protected property The parent conditional.
RulesConditionalElement::checkSiblings protected function Checks basic conditional element integrity.
RulesConditionalElement::delete public function Deletes the element and its children. 1
RulesConditionalElement::dependencies public function 1
RulesConditionalElement::exportChildren protected function 3
RulesConditionalElement::exportSettings protected function
RulesConditionalElement::getAllSibling public function Gets sibling elements.
RulesConditionalElement::getNextSibling public function Gets the next sibling element.
RulesConditionalElement::getPreviousSibling public function Gets the previous sibling element.
RulesConditionalElement::importChildren protected function 3
RulesConditionalElement::integrityCheck public function 1
RulesConditionalElement::isDefault public function Determines whether this branch is default, i.e. covers the remainder of conditions outside of all non-default branches inside the conditional. 3
RulesConditionalElement::label public function 1
RulesConditionalElement::providesVariables public function 1
RulesConditionalElement::setParent public function @todo Remove once http://drupal.org/node/1671344 is resolved.