wiki.techinc.nl/includes/search/SearchResultSetTrait.php
Aryeh Gregor c435212260 Get rid of warnings on PHP 8.1
This is mostly about adding return types to methods that implement PHP
interfaces, and not passing null to core functions that want a string.
After this patch, and an update to return types in RemexHtml,
tests/phpunit/integration/ has no more errors than in PHP 8.0.

Bug: T289879
Bug: T289926
Change-Id: Ia424f5cc897070f4188ae126b5bf6a1f552db0e1
2022-06-13 04:42:20 -04:00

60 lines
1.5 KiB
PHP

<?php
/**
* Trait useful for SearchResultSet implementations.
* It holds the functions that are rarely needed to be overridden.
*
* This trait can be used directly by extensions providing a SearchEngine.
*
* @ingroup Search
* @phan-file-suppress PhanUndeclaredMethod
*/
trait SearchResultSetTrait {
/**
* Set of result's extra data, indexed per result id
* and then per data item name.
* The structure is:
* PAGE_ID => [ augmentor name => data, ... ]
* @var array[]
*/
private $extraData = [];
/**
* Sets augmented data for result set.
* @param string $name Extra data item name
* @param array[] $data Extra data as PAGEID => data
*/
public function setAugmentedData( $name, $data ) {
foreach ( $data as $id => $resultData ) {
$this->extraData[$id][$name] = $resultData;
}
}
/**
* Returns extra data for specific result and store it in SearchResult object.
* @param SearchResult $result
*/
public function augmentResult( SearchResult $result ) {
$id = $result->getTitle()->getArticleID();
if ( $id === -1 ) {
return;
}
$result->setExtensionData( function () use ( $id ) {
return $this->extraData[$id] ?? [];
} );
}
/**
* @return int|null The offset the current page starts at. Typically
* this should be null to allow the UI to decide on its own, but in
* special cases like interleaved AB tests specifying explicitly is
* necessary.
*/
public function getOffset() {
return null;
}
final public function getIterator(): ArrayIterator {
return new ArrayIterator( $this->extractResults() );
}
}