Completed
Push — master ( 578d3a...04a929 )
by Maurício
34s queued 14s
created

JoinKeyword   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 83
rs 10
ccs 11
cts 11
cp 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A build() 0 5 3
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Components\Parsers\Conditions;
9
10
use function array_search;
11
12
/**
13
 * `JOIN` keyword parser.
14
 */
15
final class JoinKeyword implements Component
16
{
17
    /**
18
     * Types of join.
19
     */
20
    public const JOINS = [
21
        'CROSS JOIN' => 'CROSS',
22
        'FULL JOIN' => 'FULL',
23
        'FULL OUTER JOIN' => 'FULL',
24
        'INNER JOIN' => 'INNER',
25
        'JOIN' => 'JOIN',
26
        'LEFT JOIN' => 'LEFT',
27
        'LEFT OUTER JOIN' => 'LEFT',
28
        'RIGHT JOIN' => 'RIGHT',
29
        'RIGHT OUTER JOIN' => 'RIGHT',
30
        'NATURAL JOIN' => 'NATURAL',
31
        'NATURAL LEFT JOIN' => 'NATURAL LEFT',
32
        'NATURAL RIGHT JOIN' => 'NATURAL RIGHT',
33
        'NATURAL LEFT OUTER JOIN' => 'NATURAL LEFT OUTER',
34
        'NATURAL RIGHT OUTER JOIN' => 'NATURAL RIGHT OUTER',
35
        'STRAIGHT_JOIN' => 'STRAIGHT',
36
    ];
37
38
    /**
39
     * Type of this join.
40
     *
41
     * @see JoinKeyword::JOINS
42
     *
43
     * @var string
44
     */
45
    public $type;
46
47
    /**
48
     * Join expression.
49
     *
50
     * @var Expression
51
     */
52
    public $expr;
53
54
    /**
55
     * Join conditions.
56
     *
57
     * @var Condition[]
58
     */
59
    public $on;
60
61
    /**
62
     * Columns in Using clause.
63
     *
64
     * @var ArrayObj
65
     */
66
    public $using;
67
68
    /**
69
     * @see JoinKeyword::JOINS
70
     *
71
     * @param string      $type  Join type
72
     * @param Expression  $expr  join expression
73
     * @param Condition[] $on    join conditions
74
     * @param ArrayObj    $using columns joined
75
     */
76 76
    public function __construct(
77
        string|null $type = null,
78
        Expression|null $expr = null,
79
        array|null $on = null,
80
        ArrayObj|null $using = null,
81
    ) {
82 76
        $this->type = $type;
83 76
        $this->expr = $expr;
84 76
        $this->on = $on;
85 76
        $this->using = $using;
86
    }
87
88 22
    public function build(): string
89
    {
90 22
        return array_search($this->type, self::JOINS) . ' ' . $this->expr
91 22
            . (! empty($this->on) ? ' ON ' . Conditions::buildAll($this->on) : '')
92 22
            . (! empty($this->using) ? ' USING ' . $this->using->build() : '');
93
    }
94
95 22
    public function __toString(): string
96
    {
97 22
        return $this->build();
98
    }
99
}
100