You are here

abstract class RulesConditionalPredicateElement in Conditional Rules 8

Same name and namespace in other branches
  1. 7 includes/rules_conditional.core.inc \RulesConditionalPredicateElement

Base conditional element that uses a predicate.

Hierarchy

Expanded class hierarchy of RulesConditionalPredicateElement

File

includes/rules_conditional.core.inc, line 396
Conditional Rules framework implementation.

View source
abstract class RulesConditionalPredicateElement extends RulesConditionalElement {

  /**
   * @var RulesCondition
   */
  protected $predicate;
  public function __construct($predicate = NULL, $settings = array()) {
    parent::__construct();
    if (isset($predicate)) {
      $predicate = is_object($predicate) && $predicate instanceof RulesConditionInterface ? $predicate : rules_condition($predicate, $settings);
      $this
        ->setPredicate($predicate);
    }
  }

  /**
   * Sets a condition as predicate.
   */
  protected function setPredicate($predicate) {
    $this->predicate = $predicate;
    $this->predicate->parent = $this;

    // Set up variables with the new parent.
    $this
      ->resetInternalCache();
  }
  public function resetInternalCache() {
    parent::resetInternalCache();
    if (isset($this->predicate)) {
      $this->predicate
        ->resetInternalCache();
    }
  }
  public function __sleep() {
    $array = parent::__sleep();
    $array['predicate'] = 'predicate';
    return $array;
  }
  public function __clone() {
    parent::__clone();
    $this->predicate = clone $this->predicate;
    $this->predicate->parent = $this;
  }
  public function label() {
    $text = '@plugin';
    $variables = array(
      '@plugin' => $this
        ->pluginLabel(),
    );
    if (isset($this->predicate)) {
      $text = '@plugin: @label';
      $variables['@label'] = $this->predicate
        ->label();
    }
    return t($text, $variables);
  }
  public function pluginLabel() {
    return parent::label();
  }
  public function integrityCheck() {
    if (!isset($this->predicate)) {
      throw new RulesIntegrityException(t('The conditional "%plugin" does not have a valid predicate.', array(
        '%plugin' => $this
          ->plugin(),
      )), $this);
    }
    parent::integrityCheck();
    return $this;
  }

  /**
   * Adds predicate assertions to state.
   */
  protected function stateVariables($element = NULL) {
    if (!isset($element) || $element === $this->predicate) {
      return parent::stateVariables();
    }
    else {

      // Add assertions from the predicate.
      $variables = parent::stateVariables($element);
      if (isset($this->predicate) && ($assertions = $this->predicate
        ->call('variableInfoAssertions'))) {
        $variables = RulesData::addMetadataAssertions($variables, $assertions);
      }
      return $variables;
    }
  }
  public function dependencies() {
    $modules = array_flip(parent::dependencies());
    if (isset($this->predicate)) {
      $modules += array_flip($this->predicate
        ->dependencies());
    }
    return array_keys($modules);
  }

  /**
   * Negates the predicate.
   */
  public function negate($negate = TRUE) {
    $this->predicate
      ->negate($negate);
    return $this;
  }

  /**
   * Returns whether the predicate is negated.
   */
  public function isNegated() {
    return $this->predicate
      ->isNegated();
  }

  /**
   * Evaluates the predicate.
   */
  public function canEvaluate(RulesState $state) {
    return $this->predicate
      ->evaluate($state);
  }

  /**
   * Imports predicate.
   */
  protected function importChildren($export, $key = NULL) {
    $this
      ->importPredicate($export);
    parent::importChildren($export, 'DO');
  }

  /**
   * Imports predicate.
   */
  protected function importPredicate($export, $key = NULL) {
    if (!isset($key)) {
      $key = strtoupper($this
        ->plugin());
    }
    $predicate = rules_plugin_factory('condition');
    $this
      ->setPredicate($predicate);
    $predicate
      ->import($export[$key]);
  }

  /**
   * Exports predicate with actions.
   */
  protected function exportChildren($key = NULL) {
    $export = $this
      ->exportPredicate();
    return $export + parent::exportChildren('DO');
  }

  /**
   * Exports predicate.
   */
  protected function exportPredicate($key = NULL) {
    $export = array();
    if (!isset($key)) {
      $key = strtoupper($this
        ->plugin());
    }
    if (isset($this->predicate)) {
      $export[$key] = $this->predicate
        ->export();
    }
    return $export;
  }
  protected function exportFlat() {
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::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::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::providesVariables public function 1
RulesConditionalElement::setParent public function @todo Remove once http://drupal.org/node/1671344 is resolved.
RulesConditionalPredicateElement::$predicate protected property
RulesConditionalPredicateElement::canEvaluate public function Evaluates the predicate. Overrides RulesConditionalElement::canEvaluate
RulesConditionalPredicateElement::dependencies public function Overrides RulesConditionalElement::dependencies
RulesConditionalPredicateElement::exportChildren protected function Exports predicate with actions. Overrides RulesConditionalElement::exportChildren
RulesConditionalPredicateElement::exportFlat protected function
RulesConditionalPredicateElement::exportPredicate protected function Exports predicate. 1
RulesConditionalPredicateElement::importChildren protected function Imports predicate. Overrides RulesConditionalElement::importChildren
RulesConditionalPredicateElement::importPredicate protected function Imports predicate. 1
RulesConditionalPredicateElement::integrityCheck public function Overrides RulesConditionalElement::integrityCheck
RulesConditionalPredicateElement::isNegated public function Returns whether the predicate is negated.
RulesConditionalPredicateElement::label public function Overrides RulesConditionalElement::label
RulesConditionalPredicateElement::negate public function Negates the predicate.
RulesConditionalPredicateElement::pluginLabel public function 1
RulesConditionalPredicateElement::resetInternalCache public function
RulesConditionalPredicateElement::setPredicate protected function Sets a condition as predicate.
RulesConditionalPredicateElement::stateVariables protected function Adds predicate assertions to state.
RulesConditionalPredicateElement::__clone public function
RulesConditionalPredicateElement::__construct public function
RulesConditionalPredicateElement::__sleep public function