This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoctrineORMExecutor.php
115 lines (96 loc) · 3.46 KB
/
DoctrineORMExecutor.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
<?php
namespace Zenstruck\DataGridBundle\Executor;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Zenstruck\DataGridBundle\Field\FieldCollection;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class DoctrineORMExecutor implements ExecutorInterface
{
/** @var string */
protected $dqlAlias;
/** @var QueryBuilder */
protected $qb;
/**
* @param string $entity The Entity Class
* @param EntityManager $em
*/
public function __construct($entity, EntityManager $em)
{
$this->dqlAlias = 'e';
$this->qb = $em->getRepository($entity)->createQueryBuilder($this->dqlAlias);
}
public function execute(FieldCollection $fieldCollection)
{
return $this
->filterQuery($fieldCollection)
->getQuery()
->execute()
;
}
/**
* @param FieldCollection $fieldCollection
*
* @return QueryBuilder
*
* @throws \RuntimeException
*/
public function filterQuery(FieldCollection $fieldCollection)
{
if (!$this->dqlAlias) {
throw new \RuntimeException('The DQL Alias is not set.');
}
if (!$this->qb) {
throw new \RuntimeException('The QueryBuidler is not set.');
}
foreach ($fieldCollection->all() as $field) {
// do filters
if ($value = $field->getFilterValue()) {
$fieldName = $field->getName();
$method = sprintf('filter%s', ucfirst(Inflector::classify($fieldName)));
if (method_exists($this, $method)) {
// call custom method
$this->$method($value, $field);
} else {
$paramName = sprintf(':%s_field', $fieldName);
$this->qb->andWhere(sprintf('%s.%s = %s', $this->dqlAlias, $fieldName, $paramName))
->setParameter($paramName, $value);
;
}
}
// do sort
if (($order = $field->getSortDirection()) && $field->isSortable()) {
$this->qb->addOrderBy(sprintf('%s.%s', $this->dqlAlias, $field->getName()), strtoupper($order));
}
}
// do search
if ($query = $fieldCollection->getSearchQuery()) {
$method = 'filterSearchQuery';
if (method_exists($this, $method)) {
// call custom method
$this->$method($query, $fieldCollection);
} else {
// replace wildcard "*" with "%"
$query = str_replace('*', '%', $query);
if (!preg_match('/%/', $query)) {
// add wildcards to either side if none are set by user
$query = '%'.$query.'%';
}
$expressions = array();
foreach ($fieldCollection->getSearchable() as $field) {
$expressions[] = $this->qb->expr()->like(sprintf('%s.%s', $this->dqlAlias, $field->getName()), ':search_query');
}
if (count($expressions)) {
$whereExpr = call_user_func_array(array($this->qb->expr(), 'orX'), $expressions);
$this->qb
->andWhere($whereExpr)
->setParameter('search_query', $query)
;
}
}
}
return $this->qb;
}
}