Join   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 126
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getJoinConditions() 0 3 1
A on() 0 6 1
A orOn() 0 6 1
A addJoinExpression() 0 13 2
B addJoinCondition() 0 41 7
A andOn() 0 6 1
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Join.php
33
 *
34
 *  The Join class
35
 *
36
 *  @package    Platine\Database\Query
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database\Query;
48
49
use Closure;
50
51
/**
52
 * @class Join
53
 * @package Platine\Database\Query
54
 */
55
class Join
56
{
57
    /**
58
     * The Join conditions
59
     * @var array<int, mixed>
60
     */
61
    protected array $conditions = [];
62
63
    /**
64
     * @return array<int, mixed>
65
     */
66
    public function getJoinConditions(): array
67
    {
68
        return $this->conditions;
69
    }
70
71
    /**
72
     * @param string|Expression|Closure $column1
73
     * @param string|Expression|Closure|bool|null $column2
74
     * @param string $operator
75
     * @return $this
76
     */
77
    public function on(
78
        string|Expression|Closure $column1,
79
        string|Expression|Closure|bool|null $column2 = null,
80
        string $operator = '='
81
    ): self {
82
        return $this->addJoinCondition($column1, $column2, $operator, 'AND');
83
    }
84
85
    /**
86
     * @param string|Expression|Closure $column1
87
     * @param string|Expression|Closure|bool|null $column2
88
     * @param string $operator
89
     * @return $this
90
     */
91
    public function andOn(
92
        string|Expression|Closure $column1,
93
        string|Expression|Closure|bool|null $column2 = null,
94
        string $operator = '='
95
    ): self {
96
        return $this->addJoinCondition($column1, $column2, $operator, 'AND');
97
    }
98
99
    /**
100
     * @param string|Expression|Closure $column1
101
     * @param string|Expression|Closure|bool|null $column2
102
     * @param string $operator
103
     * @return $this
104
     */
105
    public function orOn(
106
        string|Expression|Closure $column1,
107
        string|Expression|Closure|bool|null $column2 = null,
108
        string $operator = '='
109
    ): self {
110
        return $this->addJoinCondition($column1, $column2, $operator, 'OR');
111
    }
112
113
    /**
114
     * @param Expression|Closure $expression
115
     * @param string $separator
116
     * @return $this
117
     */
118
    protected function addJoinExpression(Expression|Closure $expression, string $separator = 'AND'): self
119
    {
120
        if ($expression instanceof Closure) {
121
            $expression = Expression::fromClosure($expression);
122
        }
123
124
        $this->conditions[] = [
125
            'type' => 'joinExpression',
126
            'expression' => $expression,
127
            'separator' => $separator
128
        ];
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param string|Expression|Closure $column1
135
     * @param string|Expression|Closure|bool|null $column2
136
     * @param string $operator
137
     * @param string $separator
138
     * @return $this
139
     */
140
    protected function addJoinCondition(
141
        string|Expression|Closure $column1,
142
        string|Expression|Closure|bool|null $column2,
143
        string $operator,
144
        string $separator = 'AND'
145
    ): self {
146
        if ($column1 instanceof Closure) {
147
            if ($column2 === true) {
148
                return $this->addJoinExpression($column1, $separator);
149
            }
150
151
            if ($column2 === null) {
152
                $join = new Join();
153
                $column1($join);
154
155
                $this->conditions[] = [
156
                    'type' => 'joinNested',
157
                    'join' => $join,
158
                    'separator' => $separator
159
                ];
160
161
                return $this;
162
            }
163
            $column1 = Expression::fromClosure($column1);
164
        } elseif (($column1 instanceof Expression) && $column2 === true) {
165
            return $this->addJoinExpression($column1, $separator);
166
        }
167
168
        if ($column2 instanceof Closure) {
169
            $column2 = Expression::fromClosure($column2);
170
        }
171
172
        $this->conditions[] = [
173
            'type' => 'joinColumn',
174
            'column1' => $column1,
175
            'column2' => $column2,
176
            'operator' => $operator,
177
            'separator' => $separator
178
        ];
179
180
        return $this;
181
    }
182
}
183