-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryBuilderConditionGenerator.php
126 lines (101 loc) · 3.7 KB
/
QueryBuilderConditionGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\Doctrine\Orm;
use Doctrine\ORM\QueryBuilder;
use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\SearchCondition;
/**
* SearchCondition Doctrine ORM DQL ConditionGenerator.
*
* Note that only fields that have been configured with `setField()`
* will be actually used in the generated query.
*
* Keep the following in mind when using conversions.
*
* * Conversions are performed per search field and must be stateless, they receive the db-type
* and connection information for the conversion process.
* * Unlike DBAL conversions the conversion must be DQL (not SQL)
* * Values must be registered as parameters (using the ConversionHints)
* * Conversion results must be properly escaped to prevent DQL injections.
*
* After the fields are configured call `apply()` to apply
* the condition on the QueryBuilder.
*/
final class QueryBuilderConditionGenerator implements ConditionGenerator
{
/**
* @var FieldConfigBuilder
*/
private $fieldsConfig;
/**
* @var SearchCondition
*/
private $searchCondition;
/**
* @var QueryBuilder
*/
private $qb;
private $isApplied = false;
public function __construct(QueryBuilder $query, SearchCondition $searchCondition)
{
$this->fieldsConfig = new FieldConfigBuilder($query->getEntityManager(), $searchCondition->getFieldSet());
$this->searchCondition = $searchCondition;
$this->qb = $query;
}
public function setDefaultEntity(string $entity, string $alias)
{
$this->guardNotGenerated();
$this->fieldsConfig->setDefaultEntity($entity, $alias);
return $this;
}
/**
* @throws BadMethodCallException When the where-clause is already generated
*/
private function guardNotGenerated(): void
{
if ($this->isApplied) {
throw new BadMethodCallException('ConditionGenerator configuration cannot be changed once the query is applied.');
}
}
public function setField(string $fieldName, string $property, ?string $alias = null, ?string $entity = null, ?string $dbType = null)
{
$this->guardNotGenerated();
$this->fieldsConfig->setField($fieldName, $property, $alias, $entity, $dbType);
return $this;
}
public function apply(): self
{
if ($this->isApplied) {
trigger_error('SearchCondition was already applied. Ignoring operation.', \E_USER_WARNING);
return $this;
}
$this->isApplied = true;
$generator = new DqlConditionGenerator($this->qb->getEntityManager(), $this->searchCondition, $this->fieldsConfig);
$whereClause = $generator->getWhereClause();
$primaryCondition = $this->searchCondition->getPrimaryCondition();
if ($primaryCondition !== null) {
DqlConditionGenerator::applySortingTo($primaryCondition->getOrder(), $this->qb, $this->fieldsConfig);
}
DqlConditionGenerator::applySortingTo($this->searchCondition->getOrder(), $this->qb, $this->fieldsConfig);
if ($whereClause === '') {
return $this;
}
$this->qb->andWhere($whereClause);
foreach ($generator->getParameters() as $name => [$value, $type]) {
$this->qb->setParameter($name, $value, $type);
}
return $this;
}
public function getQueryBuilder(): QueryBuilder
{
return $this->qb;
}
}