|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rb\Specification\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use Rb\Specification\Doctrine\Exception\LogicException; |
|
8
|
|
|
use Rb\Specification\Doctrine\Result\ModifierInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class SpecificationRepositoryTrait. |
|
12
|
|
|
*/ |
|
13
|
|
|
trait SpecificationRepositoryTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $dqlAlias; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @see SpecificationAwareInterface::match() |
|
22
|
|
|
* |
|
23
|
|
|
* @param SpecificationInterface $specification |
|
24
|
|
|
* @param ModifierInterface|null $modifier |
|
25
|
|
|
* |
|
26
|
|
|
* @throws LogicException |
|
27
|
|
|
* |
|
28
|
|
|
* @return Query |
|
29
|
|
|
*/ |
|
30
|
|
|
public function match(SpecificationInterface $specification, ModifierInterface $modifier = null) |
|
31
|
|
|
{ |
|
32
|
|
|
if (! $specification->isSatisfiedBy($this->getEntityName())) { |
|
|
|
|
|
|
33
|
|
|
throw new LogicException(sprintf( |
|
34
|
|
|
'Specification "%s" not supported by this repository!', |
|
35
|
|
|
get_class($specification) |
|
36
|
|
|
)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$queryBuilder = $this->createQueryBuilder($this->getDqlAlias()); |
|
|
|
|
|
|
40
|
|
|
$this->modifyQueryBuilder($queryBuilder, $specification); |
|
41
|
|
|
|
|
42
|
|
|
return $this->modifyQuery($queryBuilder, $modifier); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Modifies the QueryBuilder according to the passed Specification. |
|
47
|
|
|
* Will also set the condition for this query if needed. |
|
48
|
|
|
* |
|
49
|
|
|
* @param QueryBuilder $queryBuilder |
|
50
|
|
|
* @param SpecificationInterface $specification |
|
51
|
|
|
* |
|
52
|
|
|
* @internal param string $dqlAlias |
|
53
|
|
|
*/ |
|
54
|
|
|
private function modifyQueryBuilder(QueryBuilder $queryBuilder, SpecificationInterface $specification) |
|
55
|
|
|
{ |
|
56
|
|
|
$condition = $specification->modify($queryBuilder, $this->getDqlAlias()); |
|
57
|
|
|
|
|
58
|
|
|
if (empty($condition)) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$queryBuilder->where($condition); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Modifies and returns a Query object according to the (optional) result modifier. |
|
67
|
|
|
* |
|
68
|
|
|
* @param QueryBuilder $queryBuilder |
|
69
|
|
|
* @param ModifierInterface|null $modifier |
|
70
|
|
|
* |
|
71
|
|
|
* @return Query |
|
72
|
|
|
*/ |
|
73
|
|
|
private function modifyQuery(QueryBuilder $queryBuilder, ModifierInterface $modifier = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$query = $queryBuilder->getQuery(); |
|
76
|
|
|
|
|
77
|
|
|
if ($modifier) { |
|
78
|
|
|
$modifier->modify($query); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $query; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get DQL entity alias. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getDqlAlias() |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->dqlAlias === null) { |
|
92
|
|
|
$this->dqlAlias = strtoupper($this->getEntityName()[0]); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->dqlAlias; |
|
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
Idableprovides a methodequalsIdthat 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.