1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
7
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
8
|
|
|
use LaravelFreelancerNL\Aranguent\Query\JoinClause; |
9
|
|
|
|
10
|
|
|
trait BuildsJoinClauses |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Add a join clause to the query. |
14
|
|
|
* |
15
|
|
|
* The boolean argument flag is part of this method's API in Laravel. |
16
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
17
|
|
|
* |
18
|
|
|
* @param mixed $table |
19
|
|
|
* @param \Closure|string $first |
20
|
|
|
* @param string|null $operator |
21
|
|
|
* @param string|null $second |
22
|
|
|
* @param string $type |
23
|
|
|
* @param bool $where |
24
|
|
|
* |
25
|
|
|
* @return Builder |
26
|
|
|
*/ |
27
|
14 |
|
public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): Builder |
28
|
|
|
{ |
29
|
14 |
|
$join = $this->newJoinClause($this, $type, $table); |
|
|
|
|
30
|
|
|
|
31
|
|
|
// If the first "column" of the join is really a Closure instance the developer |
32
|
|
|
// is trying to build a join with a complex "on" clause containing more than |
33
|
|
|
// one condition, so we'll add the join and call a Closure with the query. |
34
|
14 |
|
if ($first instanceof Closure) { |
35
|
|
|
$first($join); |
36
|
|
|
|
37
|
|
|
$this->joins[] = $join; |
|
|
|
|
38
|
|
|
} |
39
|
14 |
|
if (! $first instanceof Closure) { |
40
|
|
|
// If the column is simply a string, we can assume the join simply has a basic |
41
|
|
|
// "on" clause with a single condition. So we will just build the join with |
42
|
|
|
// this simple join clauses attached to it. There is not a join callback. |
43
|
|
|
|
44
|
|
|
//where and on are the same for aql |
45
|
14 |
|
$method = $where ? 'where' : 'on'; |
46
|
|
|
|
47
|
14 |
|
$this->joins[] = $join->$method($first, $operator, $second); |
48
|
|
|
} |
49
|
|
|
|
50
|
14 |
|
return $this; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get a new join clause. |
55
|
|
|
* |
56
|
|
|
* @param IlluminateQueryBuilder $parentQuery |
57
|
|
|
* @param string $type |
58
|
|
|
* @param string $table |
59
|
|
|
* |
60
|
|
|
* @return JoinClause |
61
|
|
|
*/ |
62
|
15 |
|
protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $table): JoinClause |
63
|
|
|
{ |
64
|
15 |
|
return new JoinClause($parentQuery, $type, $table); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|