1 | <?php |
||
25 | abstract class BaseSpecification implements Specification |
||
26 | { |
||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | private $context; |
||
31 | |||
32 | /** |
||
33 | * @param string|null $context |
||
34 | */ |
||
35 | public function __construct(?string $context = null) |
||
39 | |||
40 | /** |
||
41 | * @param QueryBuilder $qb |
||
42 | * @param string $context |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getFilter(QueryBuilder $qb, string $context): string |
||
56 | |||
57 | /** |
||
58 | * @param QueryBuilder $qb |
||
59 | * @param string $context |
||
60 | */ |
||
61 | public function modify(QueryBuilder $qb, string $context): void |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function filterCollection(iterable $collection, ?string $context = null): iterable |
||
74 | { |
||
75 | $spec = $this->getSpec(); |
||
76 | |||
77 | if ($spec instanceof Satisfiable) { |
||
78 | return $spec->filterCollection($collection, $this->resolveContext($context)); |
||
79 | } |
||
80 | |||
81 | return $collection; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function isSatisfiedBy($candidate, ?string $context = null): bool |
||
88 | { |
||
89 | $spec = $this->getSpec(); |
||
90 | |||
91 | if ($spec instanceof Satisfiable) { |
||
92 | return $spec->isSatisfiedBy($candidate, $this->resolveContext($context)); |
||
93 | } |
||
94 | |||
95 | return true; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Return all the specifications. |
||
100 | * |
||
101 | * @return Filter|QueryModifier |
||
102 | */ |
||
103 | abstract protected function getSpec(); |
||
104 | |||
105 | /** |
||
106 | * @param string $context |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | private function getContext(string $context): string |
||
118 | |||
119 | /** |
||
120 | * @param string $context |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | protected function getNestedContext(string $context): string |
||
132 | |||
133 | /** |
||
134 | * @param string|null $context |
||
135 | * |
||
136 | * @return string|null |
||
137 | */ |
||
138 | private function resolveContext(?string $context): ?string |
||
150 | } |
||
151 |