1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rb\Specification\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
7
|
|
|
use Rb\Specification\Doctrine\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Specification can be used as a quick-start to writing your own specifications. |
11
|
|
|
* It extends Doctrines ArrayCollection class so you can compose specifications. |
12
|
|
|
*/ |
13
|
|
|
class Specification extends ArrayCollection implements SpecificationInterface |
14
|
|
|
{ |
15
|
|
|
const AND_X = 'andX'; |
16
|
|
|
const OR_X = 'orX'; |
17
|
|
|
|
18
|
|
|
protected static $types = [self::OR_X, self::AND_X]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $type = self::AND_X; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param SpecificationInterface[] $elements |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $elements = []) |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
|
32
|
|
|
$this->setChildren($elements); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param SpecificationInterface $value |
37
|
|
|
* |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function add($value) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
if (! $value instanceof SpecificationInterface) { |
45
|
|
|
throw new InvalidArgumentException(sprintf( |
46
|
|
|
'"%s" does not implement "%s"!', |
47
|
|
|
(is_object($value)) ? get_class($value) : $value, |
48
|
|
|
SpecificationInterface::class |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return parent::add($value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function modify(QueryBuilder $queryBuilder, $dqlAlias) |
59
|
|
|
{ |
60
|
|
|
$match = function (SpecificationInterface $specification) use ($queryBuilder, $dqlAlias) { |
61
|
|
|
return $specification->modify($queryBuilder, $dqlAlias); |
62
|
|
|
}; |
63
|
|
|
|
64
|
|
|
$result = array_filter(array_map($match, $this->toArray())); |
65
|
|
|
if (empty($result)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return call_user_func_array( |
70
|
|
|
[$queryBuilder->expr(), $this->type], |
71
|
|
|
$result |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function isSatisfiedBy($value) |
79
|
|
|
{ |
80
|
|
|
/** @var SpecificationInterface $child */ |
81
|
|
|
foreach ($this as $child) { |
82
|
|
|
if ($child->isSatisfiedBy($value)) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param SpecificationInterface[] $children |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
protected function setChildren(array $children) |
98
|
|
|
{ |
99
|
|
|
$this->clear(); |
100
|
|
|
array_map([$this, 'add'], $children); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the type of comparison. |
107
|
|
|
* |
108
|
|
|
* @param string $type |
109
|
|
|
* |
110
|
|
|
* @throws InvalidArgumentException |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
protected function setType($type) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if (! in_array($type, self::$types, true)) { |
117
|
|
|
$message = sprintf('"%s" is not a valid type! Valid types: %s', $type, implode(', ', self::$types)); |
118
|
|
|
throw new InvalidArgumentException($message); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->type = $type; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.