ResultSet.php in Search API 8
File
src/Query/ResultSet.php
View source
<?php
namespace Drupal\search_api\Query;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\SearchApiException;
class ResultSet implements \IteratorAggregate, ResultSetInterface {
protected $query;
protected $resultCount = 0;
protected $resultItems = [];
protected $warnings = [];
protected $ignoredSearchKeys = [];
protected $extraData = [];
public function __construct(QueryInterface $query) {
$this->query = $query;
}
public function getQuery() {
return $this->query;
}
public function getResultCount() {
return $this->resultCount;
}
public function setResultCount($result_count) {
$this->resultCount = $result_count;
return $this;
}
public function getResultItems() {
return $this->resultItems;
}
public function addResultItem(ItemInterface $result_item) {
$this->resultItems[$result_item
->getId()] = $result_item;
return $this;
}
public function setResultItems(array $result_items) {
$this->resultItems = $result_items;
return $this;
}
public function preLoadResultItems() {
$item_ids = [];
foreach ($this->resultItems as $item_id => $object) {
try {
if (!$object
->getOriginalObject(FALSE)) {
$item_ids[] = $item_id;
}
} catch (SearchApiException $e) {
}
}
if (!$item_ids) {
return;
}
$objects = $this
->getQuery()
->getIndex()
->loadItemsMultiple($item_ids);
foreach ($objects as $item_id => $object) {
$this->resultItems[$item_id]
->setOriginalObject($object);
}
}
public function getWarnings() {
return $this->warnings;
}
public function addWarning($warning) {
$this->warnings[] = $warning;
return $this;
}
public function setWarnings(array $warnings) {
$this->warnings = $warnings;
return $this;
}
public function getIgnoredSearchKeys() {
return array_values($this->ignoredSearchKeys);
}
public function addIgnoredSearchKey($ignored_search_key) {
$this->ignoredSearchKeys[$ignored_search_key] = $ignored_search_key;
return $this;
}
public function setIgnoredSearchKeys(array $ignored_search_keys) {
$this->ignoredSearchKeys = array_combine($ignored_search_keys, $ignored_search_keys);
return $this;
}
public function hasExtraData($key) {
return array_key_exists($key, $this->extraData);
}
public function getExtraData($key, $default = NULL) {
return array_key_exists($key, $this->extraData) ? $this->extraData[$key] : $default;
}
public function &getAllExtraData() {
return $this->extraData;
}
public function setExtraData($key, $data = NULL) {
if ($data !== NULL) {
$this->extraData[$key] = $data;
}
else {
unset($this->extraData[$key]);
}
return $this;
}
public function getCloneForQuery(QueryInterface $query) {
$clone = clone $this;
$clone->query = $query;
return $clone;
}
public function getIterator() {
return new \ArrayIterator($this->resultItems);
}
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;
}
}
Classes
Name |
Description |
ResultSet |
Represents the result set of a search query. |