Passed
Push — main ( 8a18ed...1ea679 )
by Peter
02:40
created

Join::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Clause;
6
7
use QB\Generic\IQueryPart;
8
9
class Join implements IJoin
10
{
11
    public string $type;
12
    public string $tableName;
13
    public string|IQueryPart $on;
14
    public ?string $alias;
15
16
    /**
17
     * Join constructor.
18
     *
19
     * @param string            $type
20
     * @param string            $tableName
21
     * @param string|IQueryPart $on
22
     * @param string|null       $alias
23
     */
24 26
    public function __construct(string $type, string $tableName, string|IQueryPart $on, ?string $alias = null)
25
    {
26 26
        if (!in_array($type, IJoin::VALID_TYPES)) {
27
            throw new \InvalidArgumentException(sprintf('Invalid join type: %s', $type));
28
        }
29
30 26
        $this->type      = $type;
31 26
        $this->tableName = $tableName;
32 26
        $this->on        = $on;
33 26
        $this->alias     = $alias;
34 26
    }
35
36
    /**
37
     * @return string
38
     */
39 23
    public function __toString(): string
40
    {
41 23
        if ($this->alias) {
42 19
            return sprintf('%s %s AS %s ON %s', $this->type, $this->tableName, $this->alias, (string)$this->on);
43
        }
44
45 4
        return sprintf('%s %s ON %s', $this->type, $this->tableName, (string)$this->on);
46
    }
47
48
    /**
49
     * @return array
50
     */
51 5
    public function getParams(): array
52
    {
53 5
        if ($this->on instanceof IQueryPart) {
54 5
            return $this->on->getParams();
55
        }
56
57
        return [];
58
    }
59
}
60