1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Filter\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
6
|
|
|
use Doctrine\ORM\Query\Expr; |
7
|
|
|
use Doctrine\ORM\Query\Filter\SQLFilter; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\PublishableInterface; |
9
|
|
|
|
10
|
|
|
final class PublishableFilter extends SQLFilter |
11
|
|
|
{ |
12
|
|
|
/** @var Expr */ |
13
|
|
|
private $exprBuilder; |
14
|
|
|
|
15
|
|
|
public function setExpressionBuilder(Expr $exprBuilder): void |
16
|
|
|
{ |
17
|
|
|
$this->exprBuilder = $exprBuilder; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string |
21
|
|
|
{ |
22
|
|
|
if (null === $this->exprBuilder) { |
23
|
|
|
throw new \RuntimeException(sprintf('An expression builder. Be sure to call "%s::setExpressionBuilder()".', __CLASS__)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$reflection = $targetEntity->getReflectionClass(); |
27
|
|
|
if (!$reflection->implementsInterface(PublishableInterface::class)) { |
28
|
|
|
return ''; |
29
|
|
|
} |
30
|
|
|
return $this->getWhereStatement($targetTableAlias); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function getWhereStatement(string $alias): string |
34
|
|
|
{ |
35
|
|
|
$this->setParameter('published', true); |
|
|
|
|
36
|
|
|
$this->setParameter('published_date', date('Y-m-d H:i:s')); |
37
|
|
|
|
38
|
|
|
$pColumn = sprintf('%s.published', $alias); |
39
|
|
|
$stmt = '(' . $this->exprBuilder->orX( |
40
|
|
|
$this->exprBuilder->isNull($pColumn), |
41
|
|
|
$this->exprBuilder->eq($pColumn, $this->getParameter('published')) |
42
|
|
|
) . ')'; |
43
|
|
|
|
44
|
|
|
$pdColumn = sprintf('%s.published_date', $alias); |
45
|
|
|
$stmt .= ' AND (' . $this->exprBuilder->orX( |
46
|
|
|
$this->exprBuilder->isNull($pdColumn), |
47
|
|
|
$this->exprBuilder->gte($pdColumn, $this->getParameter('published_date')) |
48
|
|
|
) . ')'; |
49
|
|
|
|
50
|
|
|
return $stmt; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|