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 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 */
27class CirrusSearchIndexFieldFactory {
28
29    /**
30     * @var SearchConfig
31     */
32    private $searchConfig;
33
34    /**
35     * @param SearchConfig $searchConfig
36     */
37    public function __construct( SearchConfig $searchConfig ) {
38        $this->searchConfig = $searchConfig;
39    }
40
41    /**
42     * Create a search field definition
43     * @param string $name
44     * @param string $type
45     * @return SearchIndexField
46     */
47    public function makeSearchFieldMapping( $name, $type ): SearchIndexField {
48        // Specific type for opening_text
49        switch ( $name ) {
50            case 'opening_text':
51                return new OpeningTextIndexField( $name, $type, $this->searchConfig );
52            case 'template':
53                return new KeywordIndexField( $name, $type, $this->searchConfig, true );
54        }
55
56        switch ( $type ) {
57            case SearchIndexField::INDEX_TYPE_TEXT:
58                return new TextIndexField( $name, $type, $this->searchConfig );
59            case SearchIndexField::INDEX_TYPE_KEYWORD:
60                return new KeywordIndexField( $name, $type, $this->searchConfig );
61            case SearchIndexField::INDEX_TYPE_INTEGER:
62                return new IntegerIndexField( $name, $type, $this->searchConfig );
63            case SearchIndexField::INDEX_TYPE_NUMBER:
64                return new NumberIndexField( $name, $type, $this->searchConfig );
65            case SearchIndexField::INDEX_TYPE_DATETIME:
66                return new DatetimeIndexField( $name, $type, $this->searchConfig );
67            case SearchIndexField::INDEX_TYPE_BOOL:
68                return new BooleanIndexField( $name, $type, $this->searchConfig );
69            case SearchIndexField::INDEX_TYPE_NESTED:
70                return new NestedIndexField( $name, $type, $this->searchConfig );
71            case SearchIndexField::INDEX_TYPE_SHORT_TEXT:
72                return new ShortTextIndexField( $name, $type, $this->searchConfig );
73        }
74
75        return new NullIndexField();
76    }
77
78    /**
79     * Build a string field that does standard analysis for the language.
80     * @param string $fieldName
81     * @param int|null $options Field options:
82     *   ENABLE_NORMS: Enable norms on the field.  Good for text you search against but bad for array fields and useless
83     *     for fields that don't get involved in the score.
84     *   COPY_TO_SUGGEST: Copy the contents of this field to the suggest field for "Did you mean".
85     *   SPEED_UP_HIGHLIGHTING: Store extra data in the field to speed up highlighting.  This is important for long
86     *     strings or fields with many values.
87     * @param array $extra Extra analyzers for this field beyond the basic text and plain.
88     * @return TextIndexField definition of the field
89     */
90    public function newStringField( $fieldName, $options = null, $extra = [] ) {
91        $field = new TextIndexField(
92            $fieldName,
93            SearchIndexField::INDEX_TYPE_TEXT,
94            $this->searchConfig,
95            $extra
96        );
97
98        $field->setTextOptions( $options );
99
100        return $field;
101    }
102
103    /**
104     * Create a long field.
105     * @param string $name Field name
106     * @return IntegerIndexField
107     */
108    public function newLongField( $name ) {
109        return new IntegerIndexField(
110            $name,
111            SearchIndexField::INDEX_TYPE_INTEGER,
112            $this->searchConfig
113        );
114    }
115
116    /**
117     * Create a long field.
118     * @param string $name Field name
119     * @return KeywordIndexField
120     */
121    public function newKeywordField( $name ) {
122        return new KeywordIndexField(
123            $name,
124            SearchIndexField::INDEX_TYPE_KEYWORD,
125            $this->searchConfig
126        );
127    }
128}