1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Happyr\DoctrineSpecification; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
use Happyr\DoctrineSpecification\Filter\Filter; |
18
|
|
|
use Happyr\DoctrineSpecification\Query\QueryModifier; |
19
|
|
|
use Happyr\DoctrineSpecification\Specification\Specification; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Extend this abstract class if you want to build a new spec with your domain logic. |
23
|
|
|
*/ |
24
|
|
|
abstract class BaseSpecification implements Specification |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
private $dqlAlias; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|null $dqlAlias |
33
|
|
|
*/ |
34
|
|
|
public function __construct($dqlAlias = null) |
35
|
|
|
{ |
36
|
|
|
$this->dqlAlias = $dqlAlias; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param QueryBuilder $qb |
41
|
|
|
* @param string $dqlAlias |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getFilter(QueryBuilder $qb, $dqlAlias) |
46
|
|
|
{ |
47
|
|
|
$spec = $this->getSpec(); |
48
|
|
|
if ($spec instanceof Filter) { |
49
|
|
|
return $spec->getFilter($qb, $this->getAlias($dqlAlias)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param QueryBuilder $qb |
57
|
|
|
* @param string $dqlAlias |
58
|
|
|
*/ |
59
|
|
|
public function modify(QueryBuilder $qb, $dqlAlias) |
60
|
|
|
{ |
61
|
|
|
$spec = $this->getSpec(); |
62
|
|
|
if ($spec instanceof QueryModifier) { |
63
|
|
|
$spec->modify($qb, $this->getAlias($dqlAlias)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return all the specifications. |
69
|
|
|
* |
70
|
|
|
* @return Filter|QueryModifier|null |
71
|
|
|
*/ |
72
|
|
|
protected function getSpec() |
73
|
|
|
{ |
74
|
|
|
@trigger_error('Using the default implementation of '.__METHOD__.' method is deprecated since version 1.1 and this method will be marked as abstract in 2.0. You must overwrite this implementation.', E_USER_DEPRECATED); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $dqlAlias |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function getAlias($dqlAlias) |
85
|
|
|
{ |
86
|
|
|
if (null !== $this->dqlAlias) { |
87
|
|
|
return $this->dqlAlias; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $dqlAlias; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: