-
Notifications
You must be signed in to change notification settings - Fork 0
/
DqlConditionGenerator.php
109 lines (89 loc) · 3.12 KB
/
DqlConditionGenerator.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
<?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\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Rollerworks\Component\Search\Doctrine\Dbal\Query\QueryGenerator;
use Rollerworks\Component\Search\Doctrine\Orm\QueryPlatform\DqlQueryPlatform;
use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\SearchCondition;
use Rollerworks\Component\Search\SearchOrder;
/**
* SearchCondition Doctrine ORM DQL ConditionGenerator.
*
* This class provides the functionality for creating a DQL
* WHERE-clause based on the provided SearchCondition.
*
* Note: This class should not be used directly, use the provided ConditionGenerators instead.
*/
final class DqlConditionGenerator
{
/**
* @var SearchCondition
*/
private $searchCondition;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var string
*/
private $whereClause;
/**
* @var FieldConfigBuilder
*/
private $fieldsConfig;
/**
* @var ArrayCollection|null
*/
private $parameters;
public function __construct(EntityManagerInterface $entityManager, SearchCondition $searchCondition, FieldConfigBuilder $configBuilder)
{
$this->entityManager = $entityManager;
$this->searchCondition = $searchCondition;
$this->fieldsConfig = $configBuilder;
}
public function getWhereClause(): string
{
$fields = $this->fieldsConfig->getFields();
$connection = $this->entityManager->getConnection();
$platform = new DqlQueryPlatform($connection);
$queryGenerator = new QueryGenerator($connection, $platform, $fields);
$this->whereClause = $queryGenerator->getWhereClause($this->searchCondition);
$this->parameters = $platform->getParameters();
return $this->whereClause;
}
public function getParameters(): ArrayCollection
{
if (! isset($this->parameters)) {
throw new BadMethodCallException('getParameters() cannot be called before getWhereClause()');
}
return $this->parameters;
}
public static function applySortingTo(?SearchOrder $order, QueryBuilder $qb, FieldConfigBuilder $configBuilder): void
{
if ($order === null) {
return;
}
$fields = $configBuilder->getFields();
foreach ($order->getFields() as $fieldName => $direction) {
if (! isset($fields[$fieldName])) {
continue;
}
if (\count($fields[$fieldName]) > 1) {
throw new BadMethodCallException(\sprintf('Field "%s" is registered as multiple mapping and cannot be used for sorting.', $fieldName));
}
$qb->addOrderBy($fields[$fieldName][null]->column, mb_strtoupper($direction));
}
}
}