1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Query; |
11
|
|
|
|
12
|
|
|
use RDS\Hydrogen\Criteria\Join; |
13
|
|
|
use RDS\Hydrogen\Criteria\Relation; |
14
|
|
|
use RDS\Hydrogen\Query; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait RelationProvider |
18
|
|
|
* @mixin Query |
19
|
|
|
*/ |
20
|
|
|
trait RelationProvider |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string|array ...$relations |
24
|
|
|
* @return Query|$this|self |
25
|
|
|
*/ |
26
|
|
|
public function with(...$relations): self |
27
|
|
|
{ |
28
|
|
|
return $this->addRelation(function(string $field, \Closure $inner = null) { |
29
|
|
|
return new Relation($field, $this, $inner); |
30
|
|
|
}, ...$relations); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|array ...$relations |
35
|
|
|
* @return Query|$this|self |
36
|
|
|
*/ |
37
|
|
|
public function join(...$relations): self |
38
|
|
|
{ |
39
|
|
|
return $this->addRelation(function(string $field, \Closure $inner = null) { |
40
|
|
|
return new Join($field, $this, Join::TYPE_JOIN, $inner); |
41
|
|
|
}, ...$relations); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string|array ...$relations |
46
|
|
|
* @return Query|$this|self |
47
|
|
|
*/ |
48
|
|
|
public function leftJoin(...$relations): self |
49
|
|
|
{ |
50
|
2 |
|
return $this->addRelation(function(string $field, \Closure $inner = null) { |
51
|
2 |
|
return new Join($field, $this, Join::TYPE_LEFT_JOIN, $inner); |
52
|
2 |
|
}, ...$relations); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|array ...$relations |
57
|
|
|
* @return Query|$this|self |
58
|
|
|
*/ |
59
|
|
|
public function innerJoin(...$relations): self |
60
|
|
|
{ |
61
|
|
|
return $this->addRelation(function(string $field, \Closure $inner = null) { |
62
|
|
|
return new Join($field, $this, Join::TYPE_INNER_JOIN, $inner); |
63
|
|
|
}, ...$relations); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \Closure $onCreate |
68
|
|
|
* @param string|array ...$relations |
69
|
|
|
* @return Query|$this|self |
70
|
|
|
*/ |
71
|
2 |
|
private function addRelation(\Closure $onCreate, ...$relations): self |
72
|
|
|
{ |
73
|
2 |
|
foreach ($relations as $relation) { |
74
|
2 |
|
if (\is_string($relation)) { |
75
|
2 |
|
$this->add($onCreate($relation)); |
|
|
|
|
76
|
2 |
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (\is_array($relation)) { |
80
|
|
|
foreach ($relation as $rel => $sub) { |
81
|
|
|
\assert(\is_string($rel) && $sub instanceof \Closure); |
82
|
|
|
|
83
|
|
|
$this->add($onCreate($rel, $sub)); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$error = 'Relation should be string ("relation_name") '. |
89
|
|
|
'or array (["relation" => function]), ' . |
90
|
|
|
'but %s given'; |
91
|
|
|
|
92
|
|
|
throw new \InvalidArgumentException(\sprintf($error, \gettype($relation))); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.