Passed
Pull Request — master (#18)
by Timon
13:57 queued 02:40
created

Filter::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\Query\Operation;
5
6
use TBolier\RethinkQL\Message\MessageInterface;
7
use TBolier\RethinkQL\Query\QueryInterface;
8
use TBolier\RethinkQL\RethinkInterface;
9
use TBolier\RethinkQL\Types\Term\TermType;
10
11
class Filter extends AbstractOperation
12
{
13
    /**
14
     * @var array
15
     */
16
    private $predicate;
17
    /**
18
     * @var QueryInterface
19
     */
20
    private $query;
21
22
    /**
23
     * @param RethinkInterface $rethink
24
     * @param MessageInterface $message
25
     * @param QueryInterface $query
26
     * @param array $documents
27
     */
28 5
    public function __construct(RethinkInterface $rethink, MessageInterface $message, QueryInterface $query, array $documents) {
29 5
        parent::__construct($rethink, $message);
30
31 5
        $this->query = $query;
32 5
        $this->predicate = $documents;
33 5
        $this->rethink = $rethink;
34 5
        $this->message = $message;
35 5
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 5
    public function toArray(): array
41
    {
42 5
        $jsonDocuments = [];
43 5
        foreach ($this->predicate as $key => $document) {
44 5
            $jsonDocuments[] = json_encode($document);
45
        }
46
47
        return [
48 5
            TermType::FILTER,
49
            [
50 5
                $this->query->toArray(),
51
                [
52
                    TermType::JSON,
53 5
                    $jsonDocuments,
54
                ],
55
            ],
56
        ];
57
    }
58
}
59