Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
23 / 25 |
|
89.47% |
17 / 19 |
CRAP | |
0.00% |
0 / 1 |
CirrusSearchResultBuilder | |
92.00% |
23 / 25 |
|
89.47% |
17 / 19 |
21.23 | |
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 | |||
titleSnippet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
redirectSnippet | |
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 | |||
sectionTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
categorySnippet | |
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 MWTimestamp; |
7 | use Title; |
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 | /** |
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 $titleSnippet |
77 | * @return self |
78 | */ |
79 | public function titleSnippet( $titleSnippet ): self { |
80 | return $this->setValue( ArrayCirrusSearchResult::TITLE_SNIPPET, $titleSnippet ); |
81 | } |
82 | |
83 | /** |
84 | * @param string $redirectSnippet |
85 | * @return self |
86 | */ |
87 | public function redirectSnippet( $redirectSnippet ): self { |
88 | return $this->setValue( ArrayCirrusSearchResult::REDIRECT_SNIPPET, $redirectSnippet ); |
89 | } |
90 | |
91 | /** |
92 | * @param string $redirectTitle |
93 | * @return self |
94 | */ |
95 | public function redirectTitle( $redirectTitle ): self { |
96 | return $this->setValue( ArrayCirrusSearchResult::REDIRECT_TITLE, $redirectTitle ); |
97 | } |
98 | |
99 | /** |
100 | * @param string $sectionSnippet |
101 | * @return self |
102 | */ |
103 | public function sectionSnippet( $sectionSnippet ): self { |
104 | return $this->setValue( ArrayCirrusSearchResult::SECTION_SNIPPET, $sectionSnippet ); |
105 | } |
106 | |
107 | /** |
108 | * @param Title $sectionTitle |
109 | * @return self |
110 | */ |
111 | public function sectionTitle( Title $sectionTitle ): self { |
112 | return $this->setValue( ArrayCirrusSearchResult::SECTION_TITLE, $sectionTitle ); |
113 | } |
114 | |
115 | /** |
116 | * @param string $categorySnippet |
117 | * @return CirrusSearchResultBuilder |
118 | */ |
119 | public function categorySnippet( $categorySnippet ): self { |
120 | return $this->setValue( ArrayCirrusSearchResult::CATEGORY_SNIPPET, $categorySnippet ); |
121 | } |
122 | |
123 | /** |
124 | * @param MWTimestamp $timestamp |
125 | * @return self |
126 | */ |
127 | public function timestamp( MWTimestamp $timestamp ): self { |
128 | return $this->setValue( ArrayCirrusSearchResult::TIMESTAMP, $timestamp ); |
129 | } |
130 | |
131 | /** |
132 | * @param int $wordCount |
133 | * @return self |
134 | */ |
135 | public function wordCount( $wordCount ): self { |
136 | return $this->setValue( ArrayCirrusSearchResult::WORD_COUNT, $wordCount ); |
137 | } |
138 | |
139 | /** |
140 | * @param int $byteSize |
141 | * @return self |
142 | */ |
143 | public function byteSize( $byteSize ): self { |
144 | return $this->setValue( ArrayCirrusSearchResult::BYTE_SIZE, $byteSize ); |
145 | } |
146 | |
147 | /** |
148 | * @param string $interwikiNamespaceText |
149 | * @return self |
150 | */ |
151 | public function interwikiNamespaceText( $interwikiNamespaceText ): self { |
152 | return $this->setValue( ArrayCirrusSearchResult::INTERWIKI_NAMESPACE_TEXT, $interwikiNamespaceText ); |
153 | } |
154 | |
155 | /** |
156 | * @param bool $fileMatch |
157 | * @return self |
158 | */ |
159 | public function fileMatch( $fileMatch ) { |
160 | return $this->setValue( ArrayCirrusSearchResult::IS_FILE_MATCH, $fileMatch ); |
161 | } |
162 | |
163 | /** |
164 | * @param string $name |
165 | * @param mixed $value |
166 | */ |
167 | public function addExtraField( string $name, $value ) { |
168 | $this->data[ArrayCirrusSearchResult::EXTRA_FIELDS][$name] = $value; |
169 | } |
170 | |
171 | /** |
172 | * @param string $key |
173 | * @param mixed $value |
174 | * @param bool $failIfExisting |
175 | * @return self |
176 | */ |
177 | private function setValue( $key, $value, $failIfExisting = true ): self { |
178 | if ( $failIfExisting && isset( $this->data[$key] ) ) { |
179 | throw new InvalidArgumentException( "Value $key already set cannot overwrite" ); |
180 | } |
181 | Assert::parameter( $value !== null, '$value', 'cannot be null' ); |
182 | $this->data[$key] = $value; |
183 | return $this; |
184 | } |
185 | } |