You are here

class SearchApiQueryFilter in Search API 7

Provides a standard implementation of SearchApiQueryFilterInterface.

Hierarchy

Expanded class hierarchy of SearchApiQueryFilter

1 string reference to 'SearchApiQueryFilter'
SearchApiQuery::__construct in includes/query.inc
Constructs a new search query.

File

includes/query.inc, line 997
Contains SearchApiQueryInterface and SearchApiQuery.

View source
class SearchApiQueryFilter implements SearchApiQueryFilterInterface {

  /**
   * Array containing subfilters.
   *
   * Each of these is either an array (field, value, operator), or another
   * SearchApiFilter object.
   *
   * @var array
   */
  protected $filters;

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

  /**
   * {@inheritdoc}
   */
  public function __construct($conjunction = 'AND', array $tags = array()) {
    $this
      ->setConjunction($conjunction);
    $this->filters = array();
    $this->tags = drupal_map_assoc($tags);
  }

  /**
   * {@inheritdoc}
   */
  public function setConjunction($conjunction) {
    $this->conjunction = strtoupper(trim($conjunction)) == 'OR' ? 'OR' : 'AND';
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function filter(SearchApiQueryFilterInterface $filter) {
    $this->filters[] = $filter;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function condition($field, $value, $operator = '=') {
    $this->filters[] = array(
      $field,
      $value,
      $operator,
    );
    return $this;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function &getTags() {

    // Tags can sometimes be NULL for old serialized query filter objects.
    if (!isset($this->tags)) {
      $this->tags = array();
    }
    return $this->tags;
  }

  /**
   * Implements the magic __clone() method to clone nested filters, too.
   */
  public function __clone() {
    foreach ($this->filters as $i => $filter) {
      if (is_object($filter)) {
        $this->filters[$i] = clone $filter;
      }
    }
  }

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

    // Special case for a single, nested filter:
    if (count($this->filters) == 1 && is_object($this->filters[0])) {
      return (string) $this->filters[0];
    }
    $ret = array();
    foreach ($this->filters as $filter) {
      if (is_object($filter)) {
        $ret[] = "[\n  " . str_replace("\n", "\n    ", (string) $filter) . "\n  ]";
      }
      else {
        $ret[] = "{$filter[0]} {$filter[2]} " . str_replace("\n", "\n    ", var_export($filter[1], TRUE));
      }
    }
    return $ret ? '  ' . implode("\n{$this->conjunction}\n  ", $ret) : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiQueryFilter::$conjunction protected property String specifying this filter's conjunction ('AND' or 'OR').
SearchApiQueryFilter::$filters protected property Array containing subfilters.
SearchApiQueryFilter::condition public function Adds a new ($field $operator $value) condition. Overrides SearchApiQueryFilterInterface::condition
SearchApiQueryFilter::filter public function Adds a subfilter. Overrides SearchApiQueryFilterInterface::filter
SearchApiQueryFilter::getConjunction public function Retrieves the conjunction used by this filter. Overrides SearchApiQueryFilterInterface::getConjunction
SearchApiQueryFilter::getFilters public function Return all conditions and nested filters contained in this filter. Overrides SearchApiQueryFilterInterface::getFilters
SearchApiQueryFilter::getTags public function Retrieves the tags set on this filter. Overrides SearchApiQueryFilterInterface::getTags
SearchApiQueryFilter::hasTag public function Checks whether a certain tag was set on this filter. Overrides SearchApiQueryFilterInterface::hasTag
SearchApiQueryFilter::setConjunction public function Sets this filter's conjunction. Overrides SearchApiQueryFilterInterface::setConjunction
SearchApiQueryFilter::__clone public function Implements the magic __clone() method to clone nested filters, too.
SearchApiQueryFilter::__construct public function Constructs a new filter that uses the specified conjunction. Overrides SearchApiQueryFilterInterface::__construct
SearchApiQueryFilter::__toString public function Implements the magic __toString() method to simplify debugging.