WithClause::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Clauses;
6
7
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
8
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
9
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
10
11
class WithClause extends Clause
12
{
13
    /**
14
     * @var array<string|Expression>
15
     */
16
    protected array $collections;
17
18
    /**
19
     * WithClause constructor.
20
     *
21
     * @param array<string|Expression> $collections
22
     */
23 2
    public function __construct(
24
        array $collections
25
    ) {
26 2
        parent::__construct();
27
28 2
        $this->collections = $collections;
29
    }
30
31
    /**
32
     * @throws ExpressionTypeException
33
     */
34 2
    public function compile(QueryBuilder $queryBuilder): string
35
    {
36 2
        $collections = [];
37 2
        foreach ($this->collections as $key => $collection) {
38 2
            $collections[$key] = $queryBuilder->normalizeArgument($collection, 'Collection');
39 2
            $queryBuilder->registerCollections($collection, 'read');
40
        }
41
42 2
        $output = "WITH ";
43 2
        $implosion = '';
44 2
        foreach ($collections as $collection) {
45 2
            $implosion .= ', ' . $collection->compile($queryBuilder);
46
        }
47 2
        $output .= ltrim($implosion, ', ');
48
49 2
        return $output;
50
    }
51
}
52