Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
CirrusSearchIndexFieldFactory
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
5 / 5
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeSearchFieldMapping
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
11
 newStringField
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 newLongField
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 newKeywordField
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Search;
4
5use CirrusSearch\SearchConfig;
6use NullIndexField;
7use SearchIndexField;
8
9/**
10 * Create different types of SearchIndexFields.
11 *
12 * @license GPL-2.0-or-later
13 */
14class CirrusSearchIndexFieldFactory {
15
16    /**
17     * @var SearchConfig
18     */
19    private $searchConfig;
20
21    public function __construct( SearchConfig $searchConfig ) {
22        $this->searchConfig = $searchConfig;
23    }
24
25    /**
26     * Create a search field definition
27     * @param string $name
28     * @param string $type
29     * @return SearchIndexField
30     */
31    public function makeSearchFieldMapping( $name, $type ): SearchIndexField {
32        // Specific types
33        switch ( $name ) {
34            case 'opening_text':
35                return new OpeningTextIndexField( $name, $type, $this->searchConfig );
36            case 'template':
37                return new KeywordIndexField( $name, $type, $this->searchConfig, true );
38        }
39
40        switch ( $type ) {
41            case SearchIndexField::INDEX_TYPE_TEXT:
42                return new TextIndexField( $name, $type, $this->searchConfig );
43            case SearchIndexField::INDEX_TYPE_KEYWORD:
44                return new KeywordIndexField( $name, $type, $this->searchConfig );
45            case SearchIndexField::INDEX_TYPE_INTEGER:
46                return new IntegerIndexField( $name, $type, $this->searchConfig );
47            case SearchIndexField::INDEX_TYPE_NUMBER:
48                return new NumberIndexField( $name, $type, $this->searchConfig );
49            case SearchIndexField::INDEX_TYPE_DATETIME:
50                return new DatetimeIndexField( $name, $type, $this->searchConfig );
51            case SearchIndexField::INDEX_TYPE_BOOL:
52                return new BooleanIndexField( $name, $type, $this->searchConfig );
53            case SearchIndexField::INDEX_TYPE_NESTED:
54                return new NestedIndexField( $name, $type, $this->searchConfig );
55            case SearchIndexField::INDEX_TYPE_SHORT_TEXT:
56                return new ShortTextIndexField( $name, $type, $this->searchConfig );
57        }
58
59        return new NullIndexField();
60    }
61
62    /**
63     * Build a string field that does standard analysis for the language.
64     * @param string $fieldName
65     * @param int|null $options Field options:
66     *   ENABLE_NORMS: Enable norms on the field.  Good for text you search against but bad for array fields and useless
67     *     for fields that don't get involved in the score.
68     *   COPY_TO_SUGGEST: Copy the contents of this field to the suggest field for "Did you mean".
69     *   COPY_TO_SUGGEST_VARIANT: Copy the contents of this field to the secondary suggest field for ab testing
70     *     "Did you mean".
71     *   SPEED_UP_HIGHLIGHTING: Store extra data in the field to speed up highlighting.  This is important for long
72     *     strings or fields with many values.
73     * @param array $extra Extra analyzers for this field beyond the basic text and plain.
74     * @return TextIndexField definition of the field
75     */
76    public function newStringField( $fieldName, $options = null, $extra = [] ) {
77        $field = new TextIndexField(
78            $fieldName,
79            SearchIndexField::INDEX_TYPE_TEXT,
80            $this->searchConfig,
81            $extra
82        );
83
84        $field->setTextOptions( $options );
85
86        return $field;
87    }
88
89    /**
90     * Create a long field.
91     * @param string $name Field name
92     * @return IntegerIndexField
93     */
94    public function newLongField( $name ) {
95        return new IntegerIndexField(
96            $name,
97            SearchIndexField::INDEX_TYPE_INTEGER,
98            $this->searchConfig
99        );
100    }
101
102    /**
103     * Create a long field.
104     * @param string $name Field name
105     * @return KeywordIndexField
106     */
107    public function newKeywordField( $name ) {
108        return new KeywordIndexField(
109            $name,
110            SearchIndexField::INDEX_TYPE_KEYWORD,
111            $this->searchConfig
112        );
113    }
114}