|
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\Selection; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
17
|
|
|
use Happyr\DoctrineSpecification\Filter\Filter; |
|
18
|
|
|
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
|
19
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractSelectAs implements Selection |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Operand|Filter|string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $expression; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $alias = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Filter|Operand|string $expression |
|
35
|
|
|
* @param string $alias |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($expression, $alias) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->expression = $expression; |
|
40
|
|
|
$this->alias = $alias; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param QueryBuilder $qb |
|
45
|
|
|
* @param string $dqlAlias |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function transform(QueryBuilder $qb, $dqlAlias) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->expression instanceof Filter) { |
|
52
|
|
|
$expression = $this->expression->getFilter($qb, $dqlAlias); |
|
53
|
|
|
} else { |
|
54
|
|
|
$expression = ArgumentToOperandConverter::toField($this->expression); |
|
55
|
|
|
$expression = $expression->transform($qb, $dqlAlias); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return sprintf($this->getAliasFormat(), $expression, $this->alias); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return a select format. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
abstract protected function getAliasFormat(); |
|
67
|
|
|
} |
|
68
|
|
|
|