class Condition

Same name in this branch

Defines the condition class for the config entity query.

Hierarchy

Expanded class hierarchy of Condition

See also

\Drupal\Core\Config\Entity\Query\Query

1 string reference to 'Condition'
ConditionManager::__construct in drupal/core/lib/Drupal/Core/Condition/ConditionManager.php
Constructs aa ConditionManager object.

File

drupal/core/lib/Drupal/Core/Config/Entity/Query/Condition.php, line 19
Contains \Drupal\Core\Config\Entity\Query\Condition.

Namespace

Drupal\Core\Config\Entity\Query
View source
class Condition extends ConditionBase {

  /**
   * Implements \Drupal\Core\Entity\Query\ConditionInterface::compile().
   */
  public function compile($configs) {
    $and = strtoupper($this->conjunction) == 'AND';
    $single_conditions = array();
    $condition_groups = array();
    foreach ($this->conditions as $condition) {
      if ($condition['field'] instanceof ConditionInterface) {
        $condition_groups[] = $condition;
      }
      else {
        if (!isset($condition['operator'])) {
          $condition['operator'] = is_array($condition['value']) ? 'IN' : '=';
        }
        $single_conditions[] = $condition;
      }
    }
    $return = array();
    if ($single_conditions) {
      foreach ($configs as $config_name => $config) {
        foreach ($single_conditions as $condition) {
          $match = $this
            ->matchArray($condition, $config, explode('.', $condition['field']));

          // If AND and it's not matching, then the rest of conditions do not
          // matter and this config object does not match.
          // If OR and it is matching, then the rest of conditions do not
          // matter and this config object does match.
          if ($and != $match) {
            break;
          }
        }
        if ($match) {
          $return[$config_name] = $config;
        }
      }
    }
    elseif (!$condition_groups || $and) {

      // If there were no single conditions then either:
      // - Complex conditions, OR: need to start from no entities.
      // - Complex conditions, AND: need to start from all entities.
      // - No complex conditions (AND/OR doesn't matter): need to return all
      //   entities.
      $return = $configs;
    }
    foreach ($condition_groups as $condition) {
      $group_entities = $condition['field']
        ->compile($configs);
      if ($and) {
        $return = array_intersect_key($return, $group_entities);
      }
      else {
        $return = $return + $group_entities;
      }
    }
    return $return;
  }

  /**
   * Implements \Drupal\Core\Entity\Query\ConditionInterface::exists().
   */
  public function exists($field, $langcode = NULL) {
    return $this
      ->condition($field, NULL, 'IS NOT NULL', $langcode);
  }

  /**
   * Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists().
   */
  public function notExists($field, $langcode = NULL) {
    return $this
      ->condition($field, NULL, 'IS NULL', $langcode);
  }

  /**
   * Matches for an array representing one or more config paths.
   *
   * @param array $condition
   *   The condition array as created by the condition() method.
   * @param array $data
   *   The config array or part of it.
   * @param array $needs_matching
   *   The list of config array keys needing a match. Can contain config keys
   *   and the * wildcard.
   * @param array $parents
   *   The current list of parents.
   *
   * @return bool
   *   TRUE when the condition matched to the data else FALSE.
   */
  protected function matchArray(array $condition, array $data, array $needs_matching, array $parents = array()) {
    $parent = array_shift($needs_matching);
    $candidates = array();
    if ($parent === '*') {
      $candidates = array_keys($data);
    }
    else {

      // Avoid a notice when calling match() later.
      if (!isset($data[$parent])) {
        $data[$parent] = NULL;
      }
      $candidates = array(
        $parent,
      );
    }
    foreach ($candidates as $key) {
      if ($needs_matching) {
        if (is_array($data[$key])) {
          $new_parents = $parents;
          $new_parents[] = $key;
          if ($this
            ->matchArray($condition, $data[$key], $needs_matching, $new_parents)) {
            return TRUE;
          }
        }
      }
      elseif ($this
        ->match($condition, $data[$key])) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Perform the actual matching.
   *
   * @param array $condition
   *   The condition array as created by the condition() method.
   * @param string $value
   *   The value to match against.
   *
   * @return bool
   *   TRUE when matches else FALSE.
   */
  protected function match(array $condition, $value) {
    if (isset($value)) {
      switch ($condition['operator']) {
        case '=':
          return $value == $condition['value'];
        case '>':
          return $value > $condition['value'];
        case '<':
          return $value < $condition['value'];
        case '>=':
          return $value >= $condition['value'];
        case '<=':
          return $value <= $condition['value'];
        case 'IN':
          return array_search($value, $condition['value']) !== FALSE;
        case 'NOT IN':
          return array_search($value, $condition['value']) === FALSE;
        case 'STARTS_WITH':
          return strpos($value, $condition['value']) === 0;
        case 'CONTAINS':
          return strpos($value, $condition['value']) !== FALSE;
        case 'ENDS_WITH':
          return substr($value, -strlen($condition['value'])) === (string) $condition['value'];
        case 'IS NOT NULL':
          return TRUE;
        case 'IS NULL':
          return FALSE;
        default:
          throw new QueryException('Invalid condition operator.');
      }
    }
    return $condition['operator'] === 'IS NULL';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Condition::compile public function Implements \Drupal\Core\Entity\Query\ConditionInterface::compile(). Overrides ConditionInterface::compile
Condition::exists public function Implements \Drupal\Core\Entity\Query\ConditionInterface::exists(). Overrides ConditionInterface::exists
Condition::match protected function Perform the actual matching.
Condition::matchArray protected function Matches for an array representing one or more config paths.
Condition::notExists public function Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists(). Overrides ConditionInterface::notExists
ConditionBase::condition public function Implements \Drupal\Core\Entity\Query\ConditionInterface::compile(). Overrides ConditionInterface::condition
ConditionFundamentals::$conditions protected property Array of conditions.
ConditionFundamentals::$conjunction protected property The conjunction of this condition group. The value is one of the following:
ConditionFundamentals::conditions public function Implements \Drupal\Core\Entity\Query\ConditionInterface::conditions().
ConditionFundamentals::count public function Implements \Countable::count().
ConditionFundamentals::getConjunction public function Implements \Drupal\Core\Entity\Query\ConditionInterface::getConjunction().
ConditionFundamentals::__clone public function Implements the magic __clone function.
ConditionFundamentals::__construct public function Constructs a Condition object.