Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
8 / 10 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
SearchResultSetTrait | |
80.00% |
8 / 10 |
|
50.00% |
2 / 4 |
6.29 | |
0.00% |
0 / 1 |
setAugmentedData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
augmentResult | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
getOffset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * Trait useful for SearchResultSet implementations. |
5 | * It holds the functions that are rarely needed to be overridden. |
6 | * |
7 | * This trait can be used directly by extensions providing a SearchEngine. |
8 | * |
9 | * @ingroup Search |
10 | * @phan-file-suppress PhanUndeclaredMethod |
11 | */ |
12 | trait SearchResultSetTrait { |
13 | /** |
14 | * Set of result's extra data, indexed per result id |
15 | * and then per data item name. |
16 | * The structure is: |
17 | * PAGE_ID => [ augmentor name => data, ... ] |
18 | * @var array[] |
19 | */ |
20 | private $extraData = []; |
21 | |
22 | /** |
23 | * Sets augmented data for result set. |
24 | * @param string $name Extra data item name |
25 | * @param array[] $data Extra data as PAGEID => data |
26 | */ |
27 | public function setAugmentedData( $name, $data ) { |
28 | foreach ( $data as $id => $resultData ) { |
29 | $this->extraData[$id][$name] = $resultData; |
30 | } |
31 | } |
32 | |
33 | /** |
34 | * Returns extra data for specific result and store it in SearchResult object. |
35 | */ |
36 | public function augmentResult( SearchResult $result ) { |
37 | $id = $result->getTitle()->getArticleID(); |
38 | if ( $id === -1 ) { |
39 | return; |
40 | } |
41 | $result->setExtensionData( function () use ( $id ) { |
42 | return $this->extraData[$id] ?? []; |
43 | } ); |
44 | } |
45 | |
46 | /** |
47 | * @return int|null The offset the current page starts at. Typically |
48 | * this should be null to allow the UI to decide on its own, but in |
49 | * special cases like interleaved AB tests specifying explicitly is |
50 | * necessary. |
51 | */ |
52 | public function getOffset() { |
53 | return null; |
54 | } |
55 | |
56 | final public function getIterator(): ArrayIterator { |
57 | return new ArrayIterator( $this->extractResults() ); |
58 | } |
59 | } |