Regexp   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 119
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFlagsString() 0 11 3
A __construct() 0 5 1
A flags() 0 5 1
A maxStates() 0 5 1
A compile() 0 20 4
A name() 0 3 1
1
<?php
2
/*
3
 * This file is part of PHPinnacle/Elastics.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace PHPinnacle\Elastics\Query;
14
15
use PHPinnacle\Elastics\Query;
16
use PHPinnacle\Elastics\Traits;
17
18
class Regexp implements Query
19
{
20
    use Traits\Boost;
21
22
    const FLAGS = [
23
        'ALL'          => \ELASTICS_REGEXP_ALL,
24
        'ANYSTRING'    => \ELASTICS_REGEXP_ANYSTRING,
25
        'COMPLEMENT'   => \ELASTICS_REGEXP_COMPLEMENT,
26
        'EMPTY'        => \ELASTICS_REGEXP_EMPTY,
27
        'INTERSECTION' => \ELASTICS_REGEXP_INTERSECTION,
28
        'INTERVAL'     => \ELASTICS_REGEXP_INTERVAL,
29
    ];
30
31
    /**
32
     * @var string
33
     */
34
    private $field;
35
36
    /**
37
     * @var mixed
38
     */
39
    private $value;
40
41
    /**
42
     * @var int
43
     */
44
    private $flags;
45
46
    /**
47
     * @var int
48
     */
49
    private $maxStates;
50
51
    /**
52
     * @param string $field
53
     * @param mixed  $value
54
     * @param int    $flags
55
     */
56 4
    public function __construct(string $field, $value, int $flags = null)
57
    {
58 4
        $this->field = $field;
59 4
        $this->value = $value;
60 4
        $this->flags = $flags;
61 4
    }
62
63
    /**
64
     * @param int $flags
65
     *
66
     * @return self
67
     */
68 1
    public function flags(int $flags): self
69
    {
70 1
        $this->flags = $flags;
71
72 1
        return $this;
73
    }
74
75
    /**
76
     * @param int $maxStates
77
     *
78
     * @return self
79
     */
80 1
    public function maxStates(int $maxStates): self
81
    {
82 1
        $this->maxStates = $maxStates;
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function name(): string
91
    {
92 1
        return 'regexp';
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 3
    public function compile(): array
99
    {
100
        $query = [
101 3
            'value' => $this->value,
102
        ];
103
104 3
        if ($this->flags !== null) {
105 1
            $query['flags'] = $this->buildFlagsString($this->flags);
106
        }
107
108 3
        if ($this->maxStates !== null) {
109 1
            $query['max_determinized_states'] = $this->maxStates;
110
        }
111
112 3
        if ($this->boost !== null) {
113 1
            $query['boost'] = $this->boost;
114
        }
115
116
        return [
117 3
            $this->field => $query,
118
        ];
119
    }
120
121
    /**
122
     * @param int $flags
123
     *
124
     * @return string
125
     */
126 1
    public function buildFlagsString(int $flags): string
127
    {
128 1
        $mask = [];
129
130 1
        foreach (self::FLAGS as $key => $bit) {
131 1
            if ($flags & $bit) {
132 1
                $mask[] = $key;
133
            }
134
        }
135
136 1
        return \implode('|', $mask);
137
    }
138
}
139