You are here

abstract class RulesConditionalContainer in Conditional Rules 8

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

Base conditional statement plugin implementation.

Hierarchy

Expanded class hierarchy of RulesConditionalContainer

1 string reference to 'RulesConditionalContainer'
rules_conditional.plugin.inc in includes/rules_conditional.plugin.inc
Rules plugin implementation.

File

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

View source
abstract class RulesConditionalContainer extends RulesContainerPlugin implements RulesActionInterface {

  /**
   * Magic methods to intercept.
   * @var array
   */
  protected $interceptMethods = array();

  /**
   * @var RulesActionContainer
   */
  protected $fluentElement;
  protected $providesVariables;
  public function label() {
    $info = $this
      ->pluginInfo();
    $label = isset($info['label']) ? $info['label'] : t('unlabeled');
    return $label;
  }

  /**
   * Intercepts calls to magic methods, possibly using reserved keywords.
   */
  public function __call($name, $arguments = array()) {
    if (in_array($name, $this->interceptMethods) && method_exists($this, $mapMethod = 'call_' . $name)) {
      return call_user_func_array(array(
        $this,
        $mapMethod,
      ), $arguments);
    }
    else {
      return parent::__call($name, $arguments);
    }
  }
  public function destroy() {
    $this->fluentElement = NULL;
    parent::destroy();
  }

  /**
   * Adds an action to the active fluent statement.
   *
   * Pass either an instance of the RulesActionInterface or the arguments as
   * needed by rules_action().
   *
   * @param $name
   * @param array $settings
   * @return $this
   *   Returns $this for fluent interface.
   */
  public function action($name, array $settings = array()) {
    if (isset($this->fluentElement)) {
      $this->fluentElement
        ->action($name, $settings);
    }
    return $this;
  }

  /**
   * Evaluates the conditional statement.
   */
  public function evaluate(RulesState $state) {

    // Evaluate selected branches.
    $branches = $this
      ->selectBranches($state);
    foreach ($branches as $branch) {
      $branch
        ->evaluate($state);
    }
  }

  /**
   * Asserts no variables (since a conditional is *conditionally* evaluated).
   */
  protected function variableInfoAssertions() {
    return array();
  }

  /**
   * Declares only parent state variables for individual branches.
   *
   * By definition, divergent branches should not have each other's variables.
   */
  protected function stateVariables($element = NULL) {
    return $this
      ->availableVariables();
  }

  /**
   * Provides intersections of variables in all branches, at least one default.
   */
  public function providesVariables() {
    if (!isset($this->providesVariables)) {
      $this->providesVariables = parent::providesVariables();
      if (!$this
        ->isRoot()) {

        // Collect variables.
        $hasDefault = FALSE;
        $childVariables = array();

        /** @var $child RulesConditionalElement */
        $isEmpty = FALSE;
        foreach ($this->children as $child) {
          $hasDefault = $hasDefault || $child
            ->isDefault();
          if ($childProvides = $child
            ->providesVariables()) {
            $childVariables[] = $childProvides;
          }
          else {

            // Mark as empty if any branch does not provide variables. This is
            // to avoid having to perform intersections over empty sets.
            $isEmpty = TRUE;
            break;
          }
        }
        if ($hasDefault && !$isEmpty) {

          // Collect intersection of variable names.
          $names = NULL;
          foreach ($childVariables as $variables) {
            $newNames = array_keys($variables);
            $names = isset($names) ? array_intersect($names, $newNames) : $newNames;
          }

          // Add variables.
          if (isset($names)) {
            foreach ($names as $name) {

              // Determine if variable types are consistent.
              $type = NULL;
              foreach ($childVariables as $variables) {
                if (isset($type) && $type != $variables[$name]['type']) {
                  continue 2;
                }
                else {
                  $type = $variables[$name]['type'];
                }
              }

              // Add compatible variable.
              if (isset($type)) {
                $lastVariables = end($childVariables);
                $this->providesVariables[$name] = $lastVariables[$name];
              }
            }
          }
        }
      }
    }
    return $this->providesVariables;
  }

  /**
   * Selects the branches to evaluate for this conditional.
   *
   * @param RulesState $state
   *   Rules state to use.
   * @return RulesConditionalElement[]
   *   An array of branches to evaluate.
   */
  protected abstract function selectBranches(RulesState $state);

  /**
   * Deletes the container and its children.
   */
  public function delete($keep_children = FALSE) {
    parent::delete($keep_children);
  }
  public function dependencies() {
    $modules = array(
      'rules_conditional' => 1,
    );
    $modules += array_flip(parent::dependencies());
    return array_keys($modules);
  }
  protected function exportSettings() {
    $export = parent::exportSettings();

    // Remove provided variables as plugin is only a container.
    if (isset($export['PROVIDE'])) {
      unset($export['PROVIDE']);
    }
    return $export;
  }
  public function resetInternalCache() {
    parent::resetInternalCache();
    $this->providesVariables = NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesConditionalContainer::$fluentElement protected property
RulesConditionalContainer::$interceptMethods protected property Magic methods to intercept. 3
RulesConditionalContainer::$providesVariables protected property
RulesConditionalContainer::action public function Adds an action to the active fluent statement.
RulesConditionalContainer::delete public function Deletes the container and its children.
RulesConditionalContainer::dependencies public function
RulesConditionalContainer::destroy public function
RulesConditionalContainer::evaluate public function Evaluates the conditional statement.
RulesConditionalContainer::exportSettings protected function
RulesConditionalContainer::label public function
RulesConditionalContainer::providesVariables public function Provides intersections of variables in all branches, at least one default.
RulesConditionalContainer::resetInternalCache public function
RulesConditionalContainer::selectBranches abstract protected function Selects the branches to evaluate for this conditional. 3
RulesConditionalContainer::stateVariables protected function Declares only parent state variables for individual branches.
RulesConditionalContainer::variableInfoAssertions protected function Asserts no variables (since a conditional is *conditionally* evaluated).
RulesConditionalContainer::__call public function Intercepts calls to magic methods, possibly using reserved keywords.