1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Grid\DataSource\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
15
|
|
|
use Lug\Component\Grid\DataSource\ArrayDataSource; |
16
|
|
|
use Lug\Component\Grid\DataSource\DataSourceBuilderInterface; |
17
|
|
|
use Lug\Component\Grid\DataSource\PagerfantaDataSource; |
18
|
|
|
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository; |
19
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class DataSourceBuilder implements DataSourceBuilderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Repository |
28
|
|
|
*/ |
29
|
|
|
private $repository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var QueryBuilder |
33
|
|
|
*/ |
34
|
|
|
private $queryBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ExpressionBuilder |
38
|
|
|
*/ |
39
|
|
|
private $expressionBuilder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $limit = 10; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $page = 1; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Repository $repository |
53
|
|
|
* @param mixed[] $options |
54
|
|
|
*/ |
55
|
20 |
View Code Duplication |
public function __construct(Repository $repository, array $options = []) |
|
|
|
|
56
|
|
|
{ |
57
|
20 |
|
$repositoryMethod = isset($options['repository_method']) |
58
|
20 |
|
? $options['repository_method'] |
59
|
20 |
|
: 'createQueryBuilderForCollection'; |
60
|
|
|
|
61
|
20 |
|
$this->repository = $repository; |
62
|
20 |
|
$this->queryBuilder = $this->repository->$repositoryMethod(); |
63
|
20 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function select($select) |
69
|
|
|
{ |
70
|
1 |
|
$this->queryBuilder->addSelect($select); |
71
|
|
|
|
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function innerJoin($join, $alias) |
79
|
|
|
{ |
80
|
1 |
|
$this->queryBuilder->innerJoin($join, $alias); |
81
|
|
|
|
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
1 |
|
public function leftJoin($join, $alias) |
89
|
|
|
{ |
90
|
1 |
|
$this->queryBuilder->leftJoin($join, $alias); |
91
|
|
|
|
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function andWhere($where) |
99
|
|
|
{ |
100
|
1 |
|
$this->queryBuilder->andWhere($where); |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
1 |
|
public function orWhere($where) |
109
|
|
|
{ |
110
|
1 |
|
$this->queryBuilder->orWhere($where); |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function orderBy($sort, $order = 'ASC') |
119
|
|
|
{ |
120
|
2 |
|
$this->queryBuilder->addOrderBy($sort, $order); |
121
|
|
|
|
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function setLimit($limit) |
129
|
|
|
{ |
130
|
1 |
|
$this->limit = $limit; |
131
|
|
|
|
132
|
1 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
1 |
|
public function setPage($page) |
139
|
|
|
{ |
140
|
1 |
|
$this->page = $page; |
141
|
|
|
|
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
4 |
|
public function setParameter($parameter, $value, $type = null) |
149
|
|
|
{ |
150
|
4 |
|
$this->queryBuilder->setParameter($parameter, $value, $type); |
151
|
|
|
|
152
|
4 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
2 |
|
public function createPlaceholder($parameter, $value, $type = null) |
159
|
|
|
{ |
160
|
2 |
|
$placeholder = str_replace('.', '_', $parameter).'_'.str_replace('.', '', uniqid(null, true)); |
161
|
2 |
|
$this->setParameter($placeholder, $value, $type); |
162
|
|
|
|
163
|
2 |
|
return ':'.$placeholder; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
1 |
|
public function getProperty($field, $root = null) |
170
|
|
|
{ |
171
|
1 |
|
return $this->repository->getProperty($field, $root ?: $this->queryBuilder); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
1 |
|
public function getAliases() |
178
|
|
|
{ |
179
|
1 |
|
return $this->queryBuilder->getAllAliases(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
1 |
|
public function getExpressionBuilder() |
186
|
|
|
{ |
187
|
1 |
|
if ($this->expressionBuilder === null) { |
188
|
1 |
|
$this->expressionBuilder = new ExpressionBuilder($this->queryBuilder->expr()); |
189
|
1 |
|
} |
190
|
|
|
|
191
|
1 |
|
return $this->expressionBuilder; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
4 |
|
public function createDataSource(array $options = []) |
198
|
|
|
{ |
199
|
4 |
|
$queryBuilder = clone $this->queryBuilder; |
200
|
|
|
|
201
|
4 |
|
if (isset($options['all']) && $options['all']) { |
202
|
1 |
|
return new ArrayDataSource($queryBuilder->getQuery()->getResult()); |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
$dataSource = new PagerfantaDataSource(new DoctrineORMAdapter( |
206
|
3 |
|
$queryBuilder, |
207
|
3 |
|
isset($options['fetch_join_column']) ? $options['fetch_join_column'] : false, |
208
|
|
|
isset($options['use_output_walkers']) ? $options['use_output_walkers'] : false |
209
|
3 |
|
)); |
210
|
|
|
|
211
|
|
|
$dataSource->setMaxPerPage($this->limit); |
212
|
|
|
$dataSource->setCurrentPage($this->page); |
213
|
|
|
|
214
|
|
|
return $dataSource; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.