TermQuery::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 2
nop 2
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\TermLevel;
2
3
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasBoost;
4
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasValue;
5
6
/**
7
 * The term query finds documents that contain the exact term specified in the inverted index.
8
 *
9
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
10
 */
11
class TermQuery extends AbstractQuery
12
{
13
    use HasBoost;
14
    use HasValue;
15
16
    /**
17
     * TermQuery constructor.
18
     *
19
     * @param string|null $field
20
     * @param mixed|null  $value
21
     */
22
    public function __construct(?string $field = null, $value = null)
23
    {
24
        if ($field !== null && $value !== null) {
25
            $this->setField($field)->setValue($value);
26
        }
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function toArray()
33
    {
34
        $term = ['value' => $this->getValue()];
35
36
        $boost = $this->getBoost();
37
        if (null !== $boost) {
38
            $term['boost'] = $boost;
39
        }
40
41
        if (count($term) === 1 && isset($term['value'])) {
42
            $term = $term['value'];
43
        }
44
45
        return ['term' => [$this->getField() => $term]];
46
    }
47
}
48