MultiMatch::guardType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 14
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
use PHPinnacle\Elastics\Exception;
18
19
class MultiMatch implements Query
20
{
21
    use
22
        Traits\Match,
23
        Traits\TieBreaker
24
    ;
25
26
    /**
27
     * @var array
28
     */
29
    private $fields;
30
31
    /**
32
     * @var string
33
     */
34
    private $query;
35
36
    /**
37
     * @var string
38
     */
39
    private $type;
40
41
    /**
42
     * @param array  $fields
43
     * @param string $query
44
     * @param string $type
45
     */
46 4
    public function __construct(array $fields, string $query, string $type = null)
47
    {
48 4
        $this->guardType($type);
49
50 3
        $this->fields = $fields;
51 3
        $this->query  = $query;
52 3
        $this->type   = $type;
53 3
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function name(): string
59
    {
60 1
        return 'multi_match';
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function compile(): array
67
    {
68
        $query = [
69 2
            'fields' => $this->fields,
70 2
            'query'  => $this->query,
71
        ];
72
73 2
        if ($this->type !== null) {
74 1
            $query['type'] = $this->type;
75
        }
76
77 2
        if ($this->tieBreaker !== null) {
78 1
            $query['tie_breaker'] = $this->tieBreaker;
79
        }
80
81 2
        return $query + $this->options([
82 2
            \ELASTICS_FIELD_TRANSPOSITIONS => 'fuzzy_transpositions',
83
        ]);
84
    }
85
86
    /**
87
     * @param string $value
88
     *
89
     * @return void
90
     */
91 4
    private function guardType(string $value = null)
92
    {
93 4
        if ($value === null) {
94 3
            return;
95
        }
96
97 2
        if (!\in_array($value, [
98 2
            \ELASTICS_TYPE_BEST_FIELDS,
99
            \ELASTICS_TYPE_MOST_FIELDS,
100
            \ELASTICS_TYPE_CROSS_FIELDS,
101
            \ELASTICS_TYPE_PHRASE,
102
            \ELASTICS_TYPE_PHRASE_PREFIX,
103 2
        ], true)) {
104 1
            throw new Exception\InvalidMatchType($value);
105
        };
106 1
    }
107
}
108