You are here

class ResultSet in Search API 8

Represents the result set of a search query.

Hierarchy

Expanded class hierarchy of ResultSet

1 file declares its use of ResultSet
HighlightTest.php in tests/src/Unit/Processor/HighlightTest.php

File

src/Query/ResultSet.php, line 11

Namespace

Drupal\search_api\Query
View source
class ResultSet implements \IteratorAggregate, ResultSetInterface {

  /**
   * The executed query.
   *
   * @var \Drupal\search_api\Query\QueryInterface
   */
  protected $query;

  /**
   * The total result count.
   *
   * @var int
   */
  protected $resultCount = 0;

  /**
   * The result items.
   *
   * @var \Drupal\search_api\Item\ItemInterface[]
   */
  protected $resultItems = [];

  /**
   * A numeric array of translated, sanitized warning messages.
   *
   * @var string[]
   */
  protected $warnings = [];

  /**
   * A numeric array of search keys that were ignored.
   *
   * @var string[]
   */
  protected $ignoredSearchKeys = [];

  /**
   * Extra data set on this search result.
   *
   * @var array
   */
  protected $extraData = [];

  /**
   * Constructs a ResultSet object.
   *
   * @param \Drupal\search_api\Query\QueryInterface $query
   *   The executed query.
   */
  public function __construct(QueryInterface $query) {
    $this->query = $query;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function setResultCount($result_count) {
    $this->resultCount = $result_count;
    return $this;
  }

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

  /**
   * {@inheritdoc}
   */
  public function addResultItem(ItemInterface $result_item) {
    $this->resultItems[$result_item
      ->getId()] = $result_item;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setResultItems(array $result_items) {
    $this->resultItems = $result_items;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function preLoadResultItems() {
    $item_ids = [];
    foreach ($this->resultItems as $item_id => $object) {
      try {
        if (!$object
          ->getOriginalObject(FALSE)) {
          $item_ids[] = $item_id;
        }
      } catch (SearchApiException $e) {

        // Can't actually be thrown here, but catch for the static analyzer's
        // sake.
      }
    }
    if (!$item_ids) {
      return;
    }
    $objects = $this
      ->getQuery()
      ->getIndex()
      ->loadItemsMultiple($item_ids);
    foreach ($objects as $item_id => $object) {
      $this->resultItems[$item_id]
        ->setOriginalObject($object);
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function addWarning($warning) {
    $this->warnings[] = $warning;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setWarnings(array $warnings) {
    $this->warnings = $warnings;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getIgnoredSearchKeys() {
    return array_values($this->ignoredSearchKeys);
  }

  /**
   * {@inheritdoc}
   */
  public function addIgnoredSearchKey($ignored_search_key) {
    $this->ignoredSearchKeys[$ignored_search_key] = $ignored_search_key;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setIgnoredSearchKeys(array $ignored_search_keys) {
    $this->ignoredSearchKeys = array_combine($ignored_search_keys, $ignored_search_keys);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasExtraData($key) {
    return array_key_exists($key, $this->extraData);
  }

  /**
   * {@inheritdoc}
   */
  public function getExtraData($key, $default = NULL) {
    return array_key_exists($key, $this->extraData) ? $this->extraData[$key] : $default;
  }

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

  /**
   * {@inheritdoc}
   */
  public function setExtraData($key, $data = NULL) {
    if ($data !== NULL) {
      $this->extraData[$key] = $data;
    }
    else {
      unset($this->extraData[$key]);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCloneForQuery(QueryInterface $query) {
    $clone = clone $this;
    $clone->query = $query;
    return $clone;
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    return new \ArrayIterator($this->resultItems);
  }

  /**
   * Implements the magic __toString() method to simplify debugging.
   */
  public function __toString() {
    $out = $this
      ->getResultCount() . ' results';
    if ($this
      ->getResultItems()) {
      $out .= ':';
      foreach ($this
        ->getResultItems() as $item) {
        $item = str_replace("\n", "\n  ", "{$item}");
        $out .= "\n- " . $item;
      }
    }
    if ($this
      ->getWarnings()) {
      $out .= "\nWarnings:";
      foreach ($this
        ->getWarnings() as $warning) {
        $out .= "\n- " . $warning;
      }
    }
    if ($this
      ->getIgnoredSearchKeys()) {
      $out .= "\nIgnored keys:";
      foreach ($this
        ->getIgnoredSearchKeys() as $ignored_key) {
        $out .= "\n- " . $ignored_key;
      }
    }
    if ($this
      ->getAllExtraData()) {
      $data = str_replace("\n", "\n  ", print_r($this
        ->getAllExtraData(), TRUE));
      $out .= "\nExtra data: " . $data;
    }
    return $out;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResultSet::$extraData protected property Extra data set on this search result.
ResultSet::$ignoredSearchKeys protected property A numeric array of search keys that were ignored.
ResultSet::$query protected property The executed query.
ResultSet::$resultCount protected property The total result count.
ResultSet::$resultItems protected property The result items.
ResultSet::$warnings protected property A numeric array of translated, sanitized warning messages.
ResultSet::addIgnoredSearchKey public function Adds an ignored search key for the search query. Overrides ResultSetInterface::addIgnoredSearchKey
ResultSet::addResultItem public function Adds a new result item. Overrides ResultSetInterface::addResultItem
ResultSet::addWarning public function Adds a warning message that was triggered by the search query. Overrides ResultSetInterface::addWarning
ResultSet::getAllExtraData public function Retrieves all extra data set for this search result. Overrides ResultSetInterface::getAllExtraData
ResultSet::getCloneForQuery public function Creates a clone of this result set based on the given query. Overrides ResultSetInterface::getCloneForQuery
ResultSet::getExtraData public function Retrieves extra data for this search result. Overrides ResultSetInterface::getExtraData
ResultSet::getIgnoredSearchKeys public function Returns the ignored search keys, if any. Overrides ResultSetInterface::getIgnoredSearchKeys
ResultSet::getIterator public function
ResultSet::getQuery public function Retrieves the query executed for this search result. Overrides ResultSetInterface::getQuery
ResultSet::getResultCount public function Retrieves the total number of results that were found in this search. Overrides ResultSetInterface::getResultCount
ResultSet::getResultItems public function Retrieves the query result items. Overrides ResultSetInterface::getResultItems
ResultSet::getWarnings public function Returns the warnings triggered by the search query. Overrides ResultSetInterface::getWarnings
ResultSet::hasExtraData public function Determines whether extra data with a specific key is set on this result. Overrides ResultSetInterface::hasExtraData
ResultSet::preLoadResultItems public function Loads all "original objects" of the result items that have not been loaded. Overrides ResultSetInterface::preLoadResultItems
ResultSet::setExtraData public function Sets some extra data for this search result. Overrides ResultSetInterface::setExtraData
ResultSet::setIgnoredSearchKeys public function Sets the ignored search keys of the search query. Overrides ResultSetInterface::setIgnoredSearchKeys
ResultSet::setResultCount public function Sets the result count of the search. Overrides ResultSetInterface::setResultCount
ResultSet::setResultItems public function Sets the query result items. Overrides ResultSetInterface::setResultItems
ResultSet::setWarnings public function Sets the warnings triggered by the search query. Overrides ResultSetInterface::setWarnings
ResultSet::__construct public function Constructs a ResultSet object.
ResultSet::__toString public function Implements the magic __toString() method to simplify debugging.