Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TokenCountRouter
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 6
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 setText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFallback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setAnalyzer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addCondition
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * @license GPL-2.0-or-later
4 */
5
6namespace CirrusSearch\Extra\Query;
7
8use Elastica\Query\AbstractQuery;
9
10/**
11 * TokenCountRouter query used to trigger a particular query by counting
12 * the number of tokens in the user query.
13 *
14 * @link https://github.com/wikimedia/search-extra/blob/master/docs/token_count_router.md
15 */
16class TokenCountRouter extends AbstractQuery {
17    /**
18     * @const string greater than
19     */
20    public const GT = 'gt';
21
22    /**
23     * @const string greater or equal
24     */
25    public const GTE = 'gte';
26
27    /**
28     * @const string equal
29     */
30    public const EQ = 'eq';
31
32    /**
33     * @const string not equal
34     */
35    public const NEQ = 'neq';
36
37    /**
38     * @const string less than or equal
39     */
40    public const LTE = 'lte';
41
42    /**
43     * @const string less than
44     */
45    public const LT = 'lt';
46
47    /**
48     * @param string $text the text to analyze
49     * @param AbstractQuery $fallbackQuery the query to run when no
50     * conditions match
51     * @param string|null $field use the analyzer of this field
52     * @param string|null $analyzer use this analyzer
53     */
54    public function __construct( $text, AbstractQuery $fallbackQuery, $field = null, $analyzer = null ) {
55        $this->setText( $text );
56        $this->setFallback( $fallbackQuery );
57        if ( $field ) {
58            $this->setField( $field );
59        }
60        if ( $analyzer ) {
61            $this->setAnalyzer( $analyzer );
62        }
63    }
64
65    /**
66     * @param string $text count tokens from this text
67     * @return self
68     */
69    public function setText( $text ) {
70        return $this->setParam( 'text', $text );
71    }
72
73    /**
74     * @param AbstractQuery $query
75     * @return self
76     */
77    public function setFallback( AbstractQuery $query ) {
78        return $this->setParam( 'fallback', $query );
79    }
80
81    /**
82     * @param string $field the field to fetch analyzer info
83     * @return self
84     */
85    public function setField( $field ) {
86        return $this->setParam( 'field', $field );
87    }
88
89    /**
90     * @param string $analyzer the field to fetch analyzer info
91     * @return self
92     */
93    public function setAnalyzer( $analyzer ) {
94        return $this->setParam( 'analyzer', $analyzer );
95    }
96
97    /**
98     * Adds a new condition
99     * The first condition that evaluates to true is applied.
100     * If none match the fallback query is applied.
101     *
102     * @param string $type the condition to apply
103     * @param int $value the value to compare
104     * @param AbstractQuery $query the query to run if the condition is
105     * true ignoring all remaining conditions
106     * @return self
107     */
108    public function addCondition( $type, $value, AbstractQuery $query ) {
109        switch ( $type ) {
110            case self::GT:
111            case self::GTE:
112            case self::EQ:
113            case self::NEQ:
114            case self::LT:
115            case self::LTE:
116                break;
117            default:
118                throw new \InvalidArgumentException( "$type is not allowed as a condition type" );
119        }
120        return $this->addParam( 'conditions', [
121            $type => $value,
122            'query' => $query,
123        ] );
124    }
125}