Completed
Push — develop ( f07ae4...5325d9 )
by Sam
05:38 queued 03:06
created

RegexpQuery::setFlags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Search\Query\TermLevel;
4
5
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument;
6
use Nord\Lumen\Elasticsearch\Search\Query\TermLevel\Traits\BoostableQuery;
7
8
/**
9
 * Class RegexpQuery
10
 * @package Nord\Lumen\Elasticsearch\Search\Query\TermLevel
11
 *
12
 * @see     https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-regexp-query.html
13
 */
14
class RegexpQuery extends AbstractQuery
15
{
16
    use BoostableQuery;
17
18
    const FLAG_ALL          = 'ALL';
19
    const FLAG_ANYSTRING    = 'ANYSTRING';
20
    const FLAG_COMPLEMENT   = 'COMPLEMENT';
21
    const FLAG_EMPTY        = 'EMPTY';
22
    const FLAG_INTERSECTION = 'INTERSECTION';
23
    const FLAG_INTERVAL     = 'INTERVAL';
24
    const FLAG_NONE         = 'NONE';
25
26
    /**
27
     * @var string
28
     */
29
    private $value;
30
31
    /**
32
     * @var array
33
     */
34
    private $flags = [];
35
36
    /**
37
     * @var int
38
     */
39
    private $maxDeterminizedStates;
40
41
    /**
42
     * @inheritdoc
43
     *
44
     * @throws InvalidArgument
45
     */
46
    public function toArray()
47
    {
48
        if (!isset($this->field) || !isset($this->value)) {
49
            throw new InvalidArgument('"field" and "value" must be set for this type of query');
50
        }
51
52
        $definition = [
53
            'value' => $this->getValue(),
54
        ];
55
56
        if ($this->hasBoost()) {
57
            $definition['boost'] = $this->getBoost();
58
        }
59
60
        if (!empty($this->flags)) {
61
            $definition['flags'] = $this->getFlagsValue();
62
        }
63
64
        if ($this->maxDeterminizedStates !== null) {
65
            $definition['max_determinized_states'] = $this->maxDeterminizedStates;
66
        }
67
68
        return [
69
            'regexp' => [
70
                $this->getField() => $definition,
71
            ],
72
        ];
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getValue()
79
    {
80
        return $this->value;
81
    }
82
83
    /**
84
     * @param string $value
85
     *
86
     * @return $this
87
     */
88
    public function setValue($value)
89
    {
90
        $this->value = $value;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getFlags()
99
    {
100
        return $this->flags;
101
    }
102
103
    /**
104
     * @param array $flags
105
     *
106
     * @return RegexpQuery
107
     */
108
    public function setFlags($flags)
109
    {
110
        $this->flags = $flags;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    protected function getFlagsValue()
119
    {
120
        return implode('|', $this->flags);
121
    }
122
123
    /**
124
     * @return int
125
     */
126
    public function getMaxDeterminizedStates()
127
    {
128
        return $this->maxDeterminizedStates;
129
    }
130
131
    /**
132
     * @param int $maxDeterminizedStates
133
     *
134
     * @return RegexpQuery
135
     *
136
     * @throws InvalidArgument
137
     */
138
    public function setMaxDeterminizedStates($maxDeterminizedStates)
139
    {
140
        if (!is_int($maxDeterminizedStates)) {
141
            throw new InvalidArgument('max_determinized_states must be an integer');
142
        }
143
144
        $this->maxDeterminizedStates = $maxDeterminizedStates;
145
146
        return $this;
147
    }
148
}
149