1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Happyr\DoctrineSpecification\Query; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
use Happyr\DoctrineSpecification\Operand\Alias; |
18
|
|
|
use Happyr\DoctrineSpecification\Operand\Field; |
19
|
|
|
|
20
|
|
|
class OrderBy implements QueryModifier |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @deprecated This property will be marked as private in 2.0. |
24
|
|
|
* |
25
|
|
|
* @var Field|Alias |
26
|
|
|
*/ |
27
|
|
|
protected $field; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @deprecated This property will be marked as private in 2.0. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $order; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @deprecated This property will be marked as private in 2.0. |
38
|
|
|
* |
39
|
|
|
* @var string|null |
40
|
|
|
*/ |
41
|
|
|
protected $dqlAlias; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Field|Alias|string $field |
45
|
|
|
* @param string $order |
46
|
|
|
* @param string|null $dqlAlias |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function __construct($field, $order = 'ASC', $dqlAlias = null) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if (!($field instanceof Field) && !($field instanceof Alias)) { |
51
|
|
|
$field = new Field($field); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->field = $field; |
|
|
|
|
55
|
|
|
$this->order = $order; |
|
|
|
|
56
|
|
|
$this->dqlAlias = $dqlAlias; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param QueryBuilder $qb |
61
|
|
|
* @param string $dqlAlias |
62
|
|
|
*/ |
63
|
|
|
public function modify(QueryBuilder $qb, $dqlAlias) |
64
|
|
|
{ |
65
|
|
|
if (null !== $this->dqlAlias) { |
|
|
|
|
66
|
|
|
$dqlAlias = $this->dqlAlias; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$qb->addOrderBy($this->field->transform($qb, $dqlAlias), $this->order); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.