Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
23 / 25 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
LabelsDescriptionsFieldTrait | |
92.00% |
23 / 25 |
|
33.33% |
1 / 3 |
13.09 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getEngineHints | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
merge | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
10.01 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace Wikibase\Search\Elastic\Fields; |
6 | |
7 | use CirrusSearch\CirrusSearch; |
8 | use SearchEngine; |
9 | use SearchIndexField; |
10 | |
11 | /** |
12 | * Trait for code shared between {@link LabelsField} and {@link DescriptionsField}. |
13 | */ |
14 | trait LabelsDescriptionsFieldTrait { |
15 | |
16 | /** |
17 | * @var string |
18 | */ |
19 | protected $type; |
20 | |
21 | /** |
22 | * List of available languages |
23 | * @var string[] |
24 | */ |
25 | private $languages; |
26 | |
27 | /** |
28 | * @var array |
29 | */ |
30 | private $stemmingSettings; |
31 | |
32 | /** |
33 | * @param string[] $languages Available languages list. |
34 | * @param array $stemmingSettings Stemming config |
35 | */ |
36 | public function __construct( array $languages, array $stemmingSettings ) { |
37 | $this->languages = $languages; |
38 | /* @phan-suppress-next-line PhanTraitParentReference, PhanUndeclaredConstantOfClass */ |
39 | parent::__construct( static::NAME, SearchIndexField::INDEX_TYPE_NESTED ); |
40 | $this->stemmingSettings = $stemmingSettings; |
41 | } |
42 | |
43 | /** |
44 | * Set engine hints. |
45 | * Specifically, sets noop hint so that labels/descriptions would be compared |
46 | * as arrays and removal of labels/descriptions would be processed correctly. |
47 | * @param SearchEngine $engine |
48 | * @return array |
49 | */ |
50 | public function getEngineHints( SearchEngine $engine ) { |
51 | if ( !( $engine instanceof CirrusSearch ) ) { |
52 | // For now only Cirrus/Elastic is supported |
53 | return []; |
54 | } |
55 | return [ \CirrusSearch\Search\CirrusIndexField::NOOP_HINT => "equals" ]; |
56 | } |
57 | |
58 | /** @inheritDoc */ |
59 | public function merge( SearchIndexField $that ) { |
60 | if ( !( $that instanceof self ) || $this->type !== $that->type ) { |
61 | return false; |
62 | } |
63 | |
64 | if ( |
65 | $this->stemmingSettings == $that->stemmingSettings || |
66 | $that->stemmingSettings === [] |
67 | ) { |
68 | $mergedStemmingSettings = $this->stemmingSettings; |
69 | } elseif ( $this->stemmingSettings === [] ) { |
70 | $mergedStemmingSettings = $that->stemmingSettings; |
71 | } else { |
72 | return false; |
73 | } |
74 | |
75 | $mergedLanguages = array_values( array_unique( array_merge( |
76 | $this->languages, |
77 | $that->languages |
78 | ) ) ); |
79 | |
80 | if ( |
81 | $this->languages === $mergedLanguages && |
82 | $this->stemmingSettings === $mergedStemmingSettings |
83 | ) { |
84 | return $this; |
85 | } elseif ( |
86 | $that->languages === $mergedLanguages && |
87 | $that->stemmingSettings === $mergedStemmingSettings |
88 | ) { |
89 | return $that; |
90 | } else { |
91 | /* @phan-suppress-next-line PhanTypeInstantiateTraitStaticOrSelf */ |
92 | return new self( $mergedLanguages, $mergedStemmingSettings ); |
93 | } |
94 | } |
95 | |
96 | } |