Insert::toArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
ccs 7
cts 7
cp 1
cc 2
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\Query\AbstractQuery;
7
use TBolier\RethinkQL\Query\QueryInterface;
8
use TBolier\RethinkQL\RethinkInterface;
9
use TBolier\RethinkQL\Types\Term\TermType;
10
11
class Insert extends AbstractQuery
12
{
13
    /**
14
     * @var array
15
     */
16
    private $documents;
17
    /**
18
     * @var QueryInterface
19
     */
20
    private $query;
21
22
    public function __construct(
23
        RethinkInterface $rethink,
24
        QueryInterface $query,
25
        array $documents
26
    ) {
27
        parent::__construct($rethink);
28
29 9
        $this->query = $query;
30
        $this->documents = [$documents];
31
        $this->rethink = $rethink;
32
    }
33
34
    public function toArray(): array
35 9
    {
36
        $jsonDocuments = [];
37 9
        foreach ($this->documents as $key => $document) {
38 9
            $jsonDocuments[] = json_encode($document);
39 9
        }
40 9
41 9
        return [
42
            TermType::INSERT,
43
            [
44
                $this->query->toArray(),
45
                [
46 9
                    TermType::JSON,
47
                    $jsonDocuments,
48 9
                ],
49 9
            ],
50 9
        ];
51
    }
52
}
53