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    public function sectionTitle( Title $sectionTitle ): self {
140        return $this->setValue( ArrayCirrusSearchResult::SECTION_TITLE, $sectionTitle );
141    }
142
143    /**
144     * @param string $categorySnippet
145     * @return self
146     */
147    public function categorySnippet( $categorySnippet ): self {
148        return $this->setValue( ArrayCirrusSearchResult::CATEGORY_SNIPPET, $categorySnippet );
149    }
150
151    /**
152     * @param string $categorySnippetField
153     * @return self
154     */
155    public function categorySnippetField( $categorySnippetField ): self {
156        return $this->setValue( ArrayCirrusSearchResult::CATEGORY_SNIPPET_FIELD, $categorySnippetField );
157    }
158
159    /**
160     * @param MWTimestamp $timestamp
161     * @return self
162     */
163    public function timestamp( MWTimestamp $timestamp ): self {
164        return $this->setValue( ArrayCirrusSearchResult::TIMESTAMP, $timestamp );
165    }
166
167    /**
168     * @param int $wordCount
169     * @return self
170     */
171    public function wordCount( $wordCount ): self {
172        return $this->setValue( ArrayCirrusSearchResult::WORD_COUNT, $wordCount );
173    }
174
175    /**
176     * @param int $byteSize
177     * @return self
178     */
179    public function byteSize( $byteSize ): self {
180        return $this->setValue( ArrayCirrusSearchResult::BYTE_SIZE, $byteSize );
181    }
182
183    /**
184     * @param string $interwikiNamespaceText
185     * @return self
186     */
187    public function interwikiNamespaceText( $interwikiNamespaceText ): self {
188        return $this->setValue( ArrayCirrusSearchResult::INTERWIKI_NAMESPACE_TEXT, $interwikiNamespaceText );
189    }
190
191    /**
192     * @param bool $fileMatch
193     * @return self
194     */
195    public function fileMatch( $fileMatch ) {
196        return $this->setValue( ArrayCirrusSearchResult::IS_FILE_MATCH, $fileMatch );
197    }
198
199    /**
200     * @param string $name
201     * @param mixed $value
202     */
203    public function addExtraField( string $name, $value ) {
204        $this->data[ArrayCirrusSearchResult::EXTRA_FIELDS][$name] = $value;
205    }
206
207    /**
208     * @param string $key
209     * @param mixed $value
210     * @param bool $failIfExisting
211     * @return self
212     */
213    private function setValue( $key, $value, $failIfExisting = true ): self {
214        if ( $failIfExisting && isset( $this->data[$key] ) ) {
215            throw new InvalidArgumentException( "Value $key already set cannot overwrite" );
216        }
217        Assert::parameter( $value !== null, '$value', 'cannot be null' );
218        $this->data[$key] = $value;
219        return $this;
220    }
221}