MatchPhrase::compile()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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 MatchPhrase implements Query
19
{
20
    use Traits\Analyzer;
21
22
    /**
23
     * @var string
24
     */
25
    private $field;
26
27
    /**
28
     * @var mixed
29
     */
30
    private $query;
31
32
    /**
33
     * @var int
34
     */
35
    private $slop;
36
37
    /**
38
     * @param string $field
39
     * @param mixed  $query
40
     * @param int    $slop
41
     */
42 6
    public function __construct(string $field, $query, int $slop = null)
43
    {
44 6
        $this->field = $field;
45 6
        $this->query = $query;
46 6
        $this->slop  = $slop;
47 6
    }
48
49
    /**
50
     * @param int $slop
51
     *
52
     * @return self
53
     */
54 1
    public function slop(int $slop): self
55
    {
56 1
        $this->slop = $slop;
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function name(): string
65
    {
66 1
        return 'match_phrase';
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    public function compile(): array
73
    {
74
        $query = [
75 4
            'query' => $this->query,
76
        ];
77
78 4
        if ($this->analyzer !== null) {
79 1
            $query['analyzer'] = $this->analyzer;
80
        }
81
82 4
        if ($this->slop !== null) {
83 1
            $query['slop'] = $this->slop;
84
        }
85
86
        return [
87 4
            $this->field => $query,
88
        ];
89
    }
90
}
91