RegexpQuery::setFlags()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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