Completed
Pull Request — master (#22)
by Sergey
14:58
created

QueryBuilder::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.5293

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 18
cp 0.6111
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 2
crap 3.5293
1
<?php
2
3
namespace Isswp101\Persimmon\QueryBuilder;
4
5
use Isswp101\Persimmon\Contracts\Stringable;
6
use ONGR\ElasticsearchDSL\Search;
7
8
class QueryBuilder implements IQueryBuilder, Stringable
9
{
10
    protected $query = [];
11
    protected $chunk = 0;
12
13
    /**
14
     * @param Search|array|null $query
15
     */
16
    public function __construct($query = null)
17
    {
18
        if ($query instanceof Search) {
19
            $query = $query->toArray();
20
        }
21
        $this->query = is_array($query) ? $query : $this->getMatchAllQuery();
22 7
    }
23
24 7
    protected function getMatchAllQuery()
25 7
    {
26
        return ['query' => ['match_all' => new \stdClass()]];
27
    }
28
29
    public function build(): array
30
    {
31 1
        return $this->query;
32
    }
33 1
34 1
    public function setChunkCount(int $count): IQueryBuilder
35
    {
36
        $this->chunk = $count;
37
        return $this;
38
    }
39
40
    public function getChunkCount(): int
41 2
    {
42
        return $this->chunk;
43 2
    }
44 2
45
    public function __toString(): string
46
    {
47
        return json_encode($this->query);
48
    }
49
}
50