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