Passed
Push — main ( b4b05f...898b89 )
by Sammy
03:27 queued 01:33
created

Join::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HexMakina\Crudites\Grammar\Clause;
4
5
use HexMakina\Crudites\Grammar\Predicate;
6
7
/**
8
 * LEFT JOIN, two ways
9
 * (new Join('cbx_order', 'Orders'))->on('user_id', 'User', 'id')->type('LEFT');
10
 * (new Join('cbx_order', 'Orders'))->on('user_id', 'User', 'id')->left();
11
12
 * INNER JOIN, with helper, before on
13
 * (new Join('cbx_order', 'Orders'))->inner()->on('user_id', 'User', 'id');
14
 */
15
16
class Join extends Clause
17
{
18
    protected string $type;
19
    protected string $table;
20
    protected string $alias;
21
22
    protected ?string $column;
23
    protected ?string $referenced_table;
24
    protected ?string $referenced_column;
25
26
    public function __construct(string $table, string $alias = null)
27
    {
28
        $this->type = '';
29
        $this->table = $table;
30
        $this->alias = $alias ?? $table;
31
        
32
        $this->column = null;
33
        $this->referenced_table = null;
34
        $this->referenced_column = null;
35
    }
36
37
    public function add($nothing): self
38
    {
39
        return $this;
40
    }
41
42
    public function on(string $column, string $join_table, string $join_column): self
43
    {
44
        $this->column = $column;
45
        $this->referenced_table = $join_table;
46
        $this->referenced_column = $join_column;
47
48
        return $this;
49
    }
50
51
    public function type(string $join_type): self
52
    {
53
        $this->type = $join_type;
54
55
        return $this;
56
    }
57
58
    public function __toString(): string
59
    {
60
        if(isset($this->column, $this->referenced_table, $this->referenced_column)) {
61
            $on = (string)(new Predicate([$this->alias, $this->column], '=', [$this->referenced_table, $this->referenced_column]));
62
        }
63
        
64
        return trim(sprintf('%s JOIN `%s` `%s` ON %s', $this->type, $this->table, $this->alias, $on ?? ''));
65
    }
66
67
    /**
68
     * @return string the name of the clause
69
     */
70
    public function name(): string
71
    {
72
        return self::JOIN;
73
    }
74
75
    /**
76
     * @return string the alias of the table
77
     */
78
    public function alias(): string
79
    {
80
        return $this->alias;
81
    }
82
83
    /**
84
     * @return string the table name
85
     */
86
    public function table(): string
87
    {
88
        return $this->table;
89
    }
90
91
    public function column(): ?string
92
    {
93
        return $this->column;
94
    }
95
    /**
96
     * @return array [table, column] the referenced table and column
97
     */
98
    public function referenced(): ?array
99
    {
100
        if(isset($this->referenced_table, $this->referenced_column)) {
101
            return [$this->referenced_table, $this->referenced_column];
102
        }
103
104
        return null;
105
    }
106
}