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 = 'e'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @see SpecificationAware::match() |
22
|
|
|
* |
23
|
|
|
* @param SpecificationInterface $specification |
24
|
|
|
* @param ModifierInterface|null $modifier |
25
|
|
|
* |
26
|
|
|
* @return Query |
27
|
|
|
* @throws LogicException |
28
|
|
|
*/ |
29
|
|
|
public function match(SpecificationInterface $specification, ModifierInterface $modifier = null) |
30
|
|
|
{ |
31
|
|
|
if (! $specification->isSatisfiedBy($this->getEntityName())) { |
|
|
|
|
32
|
|
|
throw new LogicException(sprintf( |
33
|
|
|
'Specification "%s" not supported by this repository!', |
34
|
|
|
get_class($specification) |
35
|
|
|
)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$queryBuilder = $this->createQueryBuilder($this->dqlAlias); |
|
|
|
|
39
|
|
|
$this->modifyQueryBuilder($queryBuilder, $specification); |
40
|
|
|
|
41
|
|
|
return $this->modifyQuery($queryBuilder, $modifier); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Modifies the QueryBuilder according to the passed Specification. |
46
|
|
|
* Will also set the condition for this query if needed. |
47
|
|
|
* |
48
|
|
|
* @param QueryBuilder $queryBuilder |
49
|
|
|
* @param SpecificationInterface $specification |
50
|
|
|
* |
51
|
|
|
* @internal param string $dqlAlias |
52
|
|
|
*/ |
53
|
|
|
private function modifyQueryBuilder(QueryBuilder $queryBuilder, SpecificationInterface $specification) |
54
|
|
|
{ |
55
|
|
|
$condition = $specification->modify($queryBuilder, $this->dqlAlias); |
56
|
|
|
|
57
|
|
|
if (empty($condition)) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$queryBuilder->where($condition); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Modifies and returns a Query object according to the (optional) result modifier. |
66
|
|
|
* |
67
|
|
|
* @param QueryBuilder $queryBuilder |
68
|
|
|
* @param ModifierInterface|null $modifier |
69
|
|
|
* |
70
|
|
|
* @return Query |
71
|
|
|
*/ |
72
|
|
|
private function modifyQuery(QueryBuilder $queryBuilder, ModifierInterface $modifier = null) |
73
|
|
|
{ |
74
|
|
|
$query = $queryBuilder->getQuery(); |
75
|
|
|
|
76
|
|
|
if ($modifier) { |
77
|
|
|
$modifier->modify($query); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $query; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.