QueryBuilder::setTerm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Pgs\ElasticOM;
4
5
use Pgs\ElasticOM\Hydrator\Hydrator;
6
7
class QueryBuilder
8
{
9
    /** @var Adapter */
10
    private $adapter;
11
12
    /** @var string */
13
    private $entityName;
14
15
    /** @var array */
16
    private $body = [];
17
18
    /** @var Hydrator */
19
    private $hydrator;
20
21 12
    public function __construct(Adapter $adapter, string $entityName, Hydrator $hydrator)
22
    {
23 12
        $this->adapter = $adapter;
24 12
        $this->entityName = $entityName;
25 12
        $this->hydrator = $hydrator;
26 12
    }
27
28 1
    public function setMatch(string $field, string $value): self
29
    {
30 1
        $this->body['query']['match'][$field] = $value;
31
32 1
        return $this;
33
    }
34
35 1
    public function setTerm(string $field, string $value): self
36
    {
37 1
        $this->body['query']['term'][$field] = $value;
38
39 1
        return $this;
40
    }
41
42 1
    public function setRange(string $field, int $minValue, int $maxValue): self
43
    {
44 1
        $this->body['query']['range'][$field] = ['qte' => $minValue, 'lte' => $maxValue];
45
46 1
        return $this;
47
    }
48
49 1
    public function setMust(string $field, string $value): self
50
    {
51 1
        $this->body['query']['bool']['must'] = $this->setTerms($field, $value);
52
53 1
        return $this;
54
    }
55
56 1
    public function setFilter(string $field, string $value): self
57
    {
58 1
        $this->body['query']['bool']['filter'] = $this->setTerms($field, $value);
59
60 1
        return $this;
61
    }
62
63 1
    public function setMustNot(string $field, string $value): self
64
    {
65 1
        $this->body['query']['must_not']['filter'] = $this->setTerms($field, $value);
66
67 1
        return $this;
68
    }
69
70 1
    public function setShould(string $field, string $value): self
71
    {
72 1
        $this->body['query']['bool']['should'] = $this->setTerms($field, $value);
73
74 1
        return $this;
75
    }
76
77 1
    public function addOrderBy(string $sort, string $order = null): self
78
    {
79 1
        $this->body['sort'][] = [$sort => $order ?? 'asc'];
80
81 1
        return $this;
82
    }
83
84 1
    public function addGreaterThan(string $field, int $value): self
85
    {
86 1
        $this->body['query']['bool']['must'][] = ['range' => [$field => ['gt' => $value]]];
87
88 1
        return $this;
89
    }
90
91 1
    public function addLessThan(string $field, int $value): self
92
    {
93 1
        $this->body['query']['bool']['must'][] = ['range' => [$field => ['lt' => $value]]];
94
95 1
        return $this;
96
    }
97
98 1
    public function setFirstResult(int $firstResult): self
99
    {
100 1
        $this->body['from'] = $firstResult;
101
102 1
        return $this;
103
    }
104
105 1
    public function setMaxResults(int $maxResults): self
106
    {
107 1
        $this->body['size'] = $maxResults;
108
109 1
        return $this;
110
    }
111
112 12
    public function getQuery(): Query
113
    {
114 12
        if (empty($this->body['query'])) {
115 3
            $this->body['query']['match_all'] = new \stdClass();
116
        }
117
118 12
        return new Query($this->adapter, $this->entityName, $this->body, $this->hydrator);
119
    }
120
121 4
    private function setTerms(string $field, string $value)
122
    {
123 4
        $value = array_map('strtolower', preg_split('/\s+/', $value));
124 4
        if (count($value) === 1) {
125 3
            $value = reset($value);
126
        }
127
128 4
        return is_array($value)
129 1
            ? ['terms' => [$field => $value]]
130 4
            : ['term' => [$field => $value]];
131
    }
132
}
133