You are here

public function RulesConditionalContainer::providesVariables in Conditional Rules 7

Same name and namespace in other branches
  1. 8 includes/rules_conditional.core.inc \RulesConditionalContainer::providesVariables()

Provides intersections of variables in all branches, at least one default.

Overrides RulesPlugin::providesVariables

File

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

Class

RulesConditionalContainer
Base conditional statement plugin implementation.

Code

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;
}