Completed
Pull Request — master (#22)
by Sergey
23:00 queued 08:02
created

QueryBuilder::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.0625
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