Completed
Pull Request — develop (#17)
by Sam
04:04
created

RegexpQuery::toArray()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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