Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
28 / 30 |
|
91.67% |
22 / 24 |
CRAP | |
0.00% |
0 / 1 |
| CirrusSearchResultBuilder | |
93.33% |
28 / 30 |
|
91.67% |
22 / 24 |
26.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| build | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reset | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| score | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| explanation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| textSnippet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| textSnippetField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| titleSnippet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| titleSnippetField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| redirectSnippet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| redirectSnippetField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| redirectTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sectionSnippet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sectionSnippetField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sectionTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| categorySnippet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| categorySnippetField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| timestamp | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| wordCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| byteSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| interwikiNamespaceText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fileMatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addExtraField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setValue | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Search; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use MediaWiki\Title\Title; |
| 7 | use MediaWiki\Utils\MWTimestamp; |
| 8 | use Wikimedia\Assert\Assert; |
| 9 | |
| 10 | /** |
| 11 | * Helper class to build ArrayCirrusSearchResult instances |
| 12 | */ |
| 13 | class CirrusSearchResultBuilder { |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $data; |
| 18 | |
| 19 | /** |
| 20 | * @var Title |
| 21 | */ |
| 22 | private $title; |
| 23 | |
| 24 | /** |
| 25 | * @param Title $title |
| 26 | * @param string $docId |
| 27 | */ |
| 28 | public function __construct( Title $title, $docId ) { |
| 29 | $this->reset( $title, $docId ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @return ArrayCirrusSearchResult |
| 34 | */ |
| 35 | public function build() { |
| 36 | return new ArrayCirrusSearchResult( $this->title, $this->data ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Reset the current builder to reuse its instance. |
| 41 | * @param Title $title |
| 42 | * @param string $docId |
| 43 | * @return self |
| 44 | */ |
| 45 | public function reset( Title $title, $docId ): self { |
| 46 | $this->data = [ ArrayCirrusSearchResult::DOC_ID => $docId ]; |
| 47 | $this->title = $title; |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param float $score |
| 53 | * @return self |
| 54 | */ |
| 55 | public function score( $score ): self { |
| 56 | return $this->setValue( ArrayCirrusSearchResult::SCORE, $score ); |
| 57 | } |
| 58 | |
| 59 | public function explanation( array $explanation ): self { |
| 60 | return $this->setValue( ArrayCirrusSearchResult::EXPLANATION, $explanation ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param string $textSnippet |
| 65 | * @return self |
| 66 | */ |
| 67 | public function textSnippet( $textSnippet ): self { |
| 68 | return $this->setValue( ArrayCirrusSearchResult::TEXT_SNIPPET, $textSnippet ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param string $textSnippetField |
| 73 | * @return self |
| 74 | */ |
| 75 | public function textSnippetField( $textSnippetField ): self { |
| 76 | return $this->setValue( ArrayCirrusSearchResult::TEXT_SNIPPET_FIELD, $textSnippetField ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $titleSnippet |
| 81 | * @return self |
| 82 | */ |
| 83 | public function titleSnippet( $titleSnippet ): self { |
| 84 | return $this->setValue( ArrayCirrusSearchResult::TITLE_SNIPPET, $titleSnippet ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param string $titleSnippetField |
| 89 | * @return self |
| 90 | */ |
| 91 | public function titleSnippetField( $titleSnippetField ): self { |
| 92 | return $this->setValue( ArrayCirrusSearchResult::TITLE_SNIPPET_FIELD, $titleSnippetField ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param string $redirectSnippet |
| 97 | * @return self |
| 98 | */ |
| 99 | public function redirectSnippet( $redirectSnippet ): self { |
| 100 | return $this->setValue( ArrayCirrusSearchResult::REDIRECT_SNIPPET, $redirectSnippet ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param string $redirectSnippetField |
| 105 | * @return self |
| 106 | */ |
| 107 | public function redirectSnippetField( $redirectSnippetField ): self { |
| 108 | return $this->setValue( ArrayCirrusSearchResult::REDIRECT_SNIPPET_FIELD, $redirectSnippetField ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param Title|null $redirectTitle |
| 113 | * @return self |
| 114 | */ |
| 115 | public function redirectTitle( $redirectTitle ): self { |
| 116 | return $this->setValue( ArrayCirrusSearchResult::REDIRECT_TITLE, $redirectTitle ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param string $sectionSnippet |
| 121 | * @return self |
| 122 | */ |
| 123 | public function sectionSnippet( $sectionSnippet ): self { |
| 124 | return $this->setValue( ArrayCirrusSearchResult::SECTION_SNIPPET, $sectionSnippet ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param string $sectionSnippetField |
| 129 | * @return self |
| 130 | */ |
| 131 | public function sectionSnippetField( $sectionSnippetField ): self { |
| 132 | return $this->setValue( ArrayCirrusSearchResult::SECTION_SNIPPET_FIELD, $sectionSnippetField ); |
| 133 | } |
| 134 | |
| 135 | public function sectionTitle( Title $sectionTitle ): self { |
| 136 | return $this->setValue( ArrayCirrusSearchResult::SECTION_TITLE, $sectionTitle ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param string $categorySnippet |
| 141 | * @return self |
| 142 | */ |
| 143 | public function categorySnippet( $categorySnippet ): self { |
| 144 | return $this->setValue( ArrayCirrusSearchResult::CATEGORY_SNIPPET, $categorySnippet ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param string $categorySnippetField |
| 149 | * @return self |
| 150 | */ |
| 151 | public function categorySnippetField( $categorySnippetField ): self { |
| 152 | return $this->setValue( ArrayCirrusSearchResult::CATEGORY_SNIPPET_FIELD, $categorySnippetField ); |
| 153 | } |
| 154 | |
| 155 | public function timestamp( MWTimestamp $timestamp ): self { |
| 156 | return $this->setValue( ArrayCirrusSearchResult::TIMESTAMP, $timestamp ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param int $wordCount |
| 161 | * @return self |
| 162 | */ |
| 163 | public function wordCount( $wordCount ): self { |
| 164 | return $this->setValue( ArrayCirrusSearchResult::WORD_COUNT, $wordCount ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param int $byteSize |
| 169 | * @return self |
| 170 | */ |
| 171 | public function byteSize( $byteSize ): self { |
| 172 | return $this->setValue( ArrayCirrusSearchResult::BYTE_SIZE, $byteSize ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param string $interwikiNamespaceText |
| 177 | * @return self |
| 178 | */ |
| 179 | public function interwikiNamespaceText( $interwikiNamespaceText ): self { |
| 180 | return $this->setValue( ArrayCirrusSearchResult::INTERWIKI_NAMESPACE_TEXT, $interwikiNamespaceText ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param bool $fileMatch |
| 185 | * @return self |
| 186 | */ |
| 187 | public function fileMatch( $fileMatch ) { |
| 188 | return $this->setValue( ArrayCirrusSearchResult::IS_FILE_MATCH, $fileMatch ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param string $name |
| 193 | * @param mixed $value |
| 194 | */ |
| 195 | public function addExtraField( string $name, $value ) { |
| 196 | $this->data[ArrayCirrusSearchResult::EXTRA_FIELDS][$name] = $value; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param string $key |
| 201 | * @param mixed $value |
| 202 | * @param bool $failIfExisting |
| 203 | * @return self |
| 204 | */ |
| 205 | private function setValue( $key, $value, $failIfExisting = true ): self { |
| 206 | if ( $failIfExisting && isset( $this->data[$key] ) ) { |
| 207 | throw new InvalidArgumentException( "Value $key already set cannot overwrite" ); |
| 208 | } |
| 209 | Assert::parameter( $value !== null, '$value', 'cannot be null' ); |
| 210 | $this->data[$key] = $value; |
| 211 | return $this; |
| 212 | } |
| 213 | } |