You are here

class ConditionGroup in Search API 8

Provides a standard implementation for a Search API query condition group.

Hierarchy

Expanded class hierarchy of ConditionGroup

2 files declare their use of ConditionGroup
AllTermsArgumentTest.php in tests/src/Unit/Views/AllTermsArgumentTest.php
SearchApiQuery.php in src/Plugin/views/query/SearchApiQuery.php

File

src/Query/ConditionGroup.php, line 8

Namespace

Drupal\search_api\Query
View source
class ConditionGroup implements ConditionGroupInterface {

  /**
   * Array containing sub-conditions.
   *
   * Each of these is either an array (field, value, operator), or another
   * \Drupal\search_api\Query\ConditionGroupInterface object.
   *
   * @var array
   */
  protected $conditions = [];

  /**
   * String specifying this condition group's conjunction ('AND' or 'OR').
   *
   * @var string
   */
  protected $conjunction;

  /**
   * An array of tags set on this condition group.
   *
   * @var string[]
   */
  protected $tags;

  /**
   * Constructs a ConditionGroup object.
   *
   * @param string $conjunction
   *   (optional) The conjunction to use for this condition group - either 'AND'
   *   or 'OR'.
   * @param string[] $tags
   *   (optional) An arbitrary set of tags. Can be used to identify this
   *   condition group after it's been added to the query. This is
   *   primarily used by the facet system to support OR facet queries.
   */
  public function __construct($conjunction = 'AND', array $tags = []) {
    $this->conjunction = strtoupper(trim($conjunction)) == 'OR' ? 'OR' : 'AND';
    $this->tags = array_combine($tags, $tags);
  }

  /**
   * {@inheritdoc}
   */
  public function getConjunction() {
    return $this->conjunction;
  }

  /**
   * {@inheritdoc}
   */
  public function addConditionGroup(ConditionGroupInterface $condition_group) {
    $this->conditions[] = $condition_group;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function addCondition($field, $value, $operator = '=') {
    $this->conditions[] = new Condition($field, $value, $operator);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function &getConditions() {
    return $this->conditions;
  }

  /**
   * {@inheritdoc}
   */
  public function hasTag($tag) {
    return isset($this->tags[$tag]);
  }

  /**
   * {@inheritdoc}
   */
  public function &getTags() {
    return $this->tags;
  }

  /**
   * Implements the magic __clone() method.
   *
   * Takes care to clone nested conditions and condition groups, too.
   */
  public function __clone() {
    foreach ($this->conditions as $i => $condition) {
      $this->conditions[$i] = clone $condition;
    }
  }

  /**
   * Implements the magic __toString() method to simplify debugging.
   */
  public function __toString() {

    // Special case for a single, nested condition group:
    if (count($this->conditions) == 1) {
      return (string) reset($this->conditions);
    }
    $ret = [];
    foreach ($this->conditions as $condition) {
      $ret[] = str_replace("\n", "\n  ", (string) $condition);
    }
    return $ret ? "[\n  " . implode("\n{$this->conjunction}\n  ", $ret) . "\n]" : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionGroup::$conditions protected property Array containing sub-conditions.
ConditionGroup::$conjunction protected property String specifying this condition group's conjunction ('AND' or 'OR').
ConditionGroup::$tags protected property An array of tags set on this condition group.
ConditionGroup::addCondition public function Adds a new ($field $operator $value) condition. Overrides ConditionSetInterface::addCondition
ConditionGroup::addConditionGroup public function Adds a nested condition group. Overrides ConditionSetInterface::addConditionGroup
ConditionGroup::getConditions public function Retrieves all conditions and nested condition groups of this object. Overrides ConditionGroupInterface::getConditions
ConditionGroup::getConjunction public function Retrieves the conjunction used by this condition group. Overrides ConditionGroupInterface::getConjunction
ConditionGroup::getTags public function Retrieves the tags set on this condition group. Overrides ConditionGroupInterface::getTags
ConditionGroup::hasTag public function Checks whether a certain tag was set on this condition group. Overrides ConditionGroupInterface::hasTag
ConditionGroup::__clone public function Implements the magic __clone() method.
ConditionGroup::__construct public function Constructs a ConditionGroup object.
ConditionGroup::__toString public function Implements the magic __toString() method to simplify debugging.