JoinTrait::innerJoin()   A
last analyzed

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
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Enum\JoinEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
7
8
trait JoinTrait
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $join = [];
14
15
    /**
16
     * @param string $join
17
     * @param string $tableName
18
     * @param string $onCondition
19
     * @param array $bind
20
     *
21
     * @return $this
22
     * @throws QueryBuilderException
23
     */
24
    public function join(string $join, string $tableName, string $onCondition, array $bind = [])
25
    {
26
        if (!\in_array($join, JoinEnum::JOINS)) {
27
            throw new QueryBuilderException(\sprintf('$join "%s" is not a valid join type', $join));
28
        }
29
30
        if (empty($tableName)) {
31
            throw new QueryBuilderException('You must pass $table name to join method!');
32
        }
33
34
        if (empty($onCondition)) {
35
            throw new QueryBuilderException('You must pass $onCondition name to join method!');
36
        }
37
38
        $joinString = \sprintf('%s %s ON (%s)', $join, $tableName, $onCondition);
39
40
        if (\array_search($joinString, $this->join, false) === false) {
41
            $this->join[] = $joinString;
42
        }
43
44
        if (!empty($bind)) {
45
            $this->bind($bind);
0 ignored issues
show
Bug introduced by
It seems like bind() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            $this->/** @scrutinizer ignore-call */ 
46
                   bind($bind);
Loading history...
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $join
53
     * @param string $tableName
54
     * @param string $alias
55
     * @param string $onCondition
56
     * @param array $bind
57
     *
58
     * @return $this
59
     * @throws QueryBuilderException
60
     */
61
    public function joinAs(string $join, string $tableName, string $alias, string $onCondition, array $bind = [])
62
    {
63
        if (empty($alias)) {
64
            throw new QueryBuilderException('You must pass $alias name to join method!');
65
        }
66
67
        return $this->join($join, \sprintf('%s AS %s', $tableName, $alias), $onCondition, $bind);
68
    }
69
70
    /**
71
     * @param string $tableName
72
     * @param string $onCondition
73
     * @param array $bind
74
     *
75
     * @return $this
76
     */
77
    public function innerJoin(string $tableName, string $onCondition, array $bind = [])
78
    {
79
        return $this->join(JoinEnum::INNER_JOIN, $tableName, $onCondition, $bind);
80
    }
81
82
    /**
83
     * @param string $tableName
84
     * @param string $onCondition
85
     * @param string $alias
86
     * @param array $bind
87
     *
88
     * @return $this
89
     */
90
    public function innerJoinAs(string $tableName, string $alias, string $onCondition, array $bind = [])
91
    {
92
        return $this->joinAs(JoinEnum::INNER_JOIN, $tableName, $alias, $onCondition, $bind);
93
    }
94
95
    /**
96
     * @param string $tableName
97
     * @param string $onCondition
98
     * @param array $bind
99
     *
100
     * @return $this
101
     */
102
    public function leftJoin(string $tableName, string $onCondition, array $bind = [])
103
    {
104
        return $this->join(JoinEnum::LEFT_JOIN, $tableName, $onCondition, $bind);
105
    }
106
107
    /**
108
     * @param string $tableName
109
     * @param string $onCondition
110
     * @param string $alias
111
     * @param array $bind
112
     *
113
     * @return $this
114
     */
115
    public function leftJoinAs(string $tableName, string $alias, string $onCondition, array $bind = [])
116
    {
117
        return $this->joinAs(JoinEnum::LEFT_JOIN, $tableName, $alias, $onCondition, $bind);
118
    }
119
120
    /**
121
     * @param string $tableName
122
     * @param string $onCondition
123
     * @param array $bind
124
     *
125
     * @return $this
126
     */
127
    public function rightJoin(string $tableName, string $onCondition, array $bind = [])
128
    {
129
        return $this->join(JoinEnum::RIGHT_JOIN, $tableName, $onCondition, $bind);
130
    }
131
132
    /**
133
     * @param string $tableName
134
     * @param string $alias
135
     * @param string $onCondition
136
     * @param array $bind
137
     *
138
     * @return $this
139
     */
140
    public function rightJoinAs(string $tableName, string $alias, string $onCondition, array $bind = [])
141
    {
142
        return $this->joinAs(JoinEnum::RIGHT_JOIN, $tableName, $alias, $onCondition, $bind);
143
    }
144
145
    /**
146
     * @param string $tableName
147
     * @param string $onCondition
148
     * @param array $bind
149
     *
150
     * @return $this
151
     */
152
    public function crossJoin(string $tableName, string $onCondition, array $bind = [])
153
    {
154
        return $this->join(JoinEnum::CROSS_JOIN, $tableName, $onCondition, $bind);
155
    }
156
157
    /**
158
     * @param string $tableName
159
     * @param string $onCondition
160
     * @param string $alias
161
     * @param array $bind
162
     *
163
     * @return $this
164
     */
165
    public function crossJoinAs(string $tableName, string $alias, string $onCondition, array $bind = [])
166
    {
167
        return $this->joinAs(JoinEnum::CROSS_JOIN, $tableName, $alias, $onCondition, $bind);
168
    }
169
170
    /**
171
     * @param string $tableName
172
     * @param string $onCondition
173
     * @param array $bind
174
     *
175
     * @return $this
176
     */
177
    public function fullOuterJoin(string $tableName, string $onCondition, array $bind = [])
178
    {
179
        return $this->join(JoinEnum::FULL_OUTER_JOIN, $tableName, $onCondition, $bind);
180
    }
181
182
    /**
183
     * @param string $tableName
184
     * @param string $alias
185
     * @param string $onCondition
186
     * @param array $bind
187
     *
188
     * @return $this
189
     */
190
    public function fullOuterJoinAs(string $tableName, string $alias, string $onCondition, array $bind = [])
191
    {
192
        return $this->joinAs(JoinEnum::FULL_OUTER_JOIN, $tableName, $alias, $onCondition, $bind);
193
    }
194
195
    /**
196
     * @return null|string
197
     */
198
    protected function buildJoinQueryPart(): ?string
199
    {
200
        return empty($this->join) ? null : \implode(' ', $this->join);
201
    }
202
}
203