Completed
Push — 1.0 ( 75b22b...f155d9 )
by Peter
15:56 queued 05:56
created

AbstractSelectAs::getAliasFormat()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Happyr\DoctrineSpecification\Query\Selection;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Happyr\DoctrineSpecification\Filter\Filter;
7
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
8
use Happyr\DoctrineSpecification\Operand\Operand;
9
10
abstract class AbstractSelectAs implements Selection
11
{
12
    /**
13
     * @var Operand|Filter|string
14
     */
15
    private $expression;
16
17
    /**
18
     * @var string
19
     */
20
    private $alias = '';
21
22
    /**
23
     * @param Filter|Operand|string $expression
24
     * @param string                $alias
25
     */
26
    public function __construct($expression, $alias)
27
    {
28
        $this->expression = $expression;
29
        $this->alias = $alias;
30
    }
31
32
    /**
33
     * @param QueryBuilder $qb
34
     * @param string       $dqlAlias
35
     *
36
     * @return string
37
     */
38
    public function transform(QueryBuilder $qb, $dqlAlias)
39
    {
40
        if ($this->expression instanceof Filter) {
41
            $expression = $this->expression->getFilter($qb, $dqlAlias);
42
        } else {
43
            $expression = ArgumentToOperandConverter::toField($this->expression);
44
            $expression = $expression->transform($qb, $dqlAlias);
45
        }
46
47
        return sprintf($this->getAliasFormat(), $expression, $this->alias);
48
    }
49
50
    /**
51
     * Return a select format.
52
     *
53
     * @return string
54
     */
55
    abstract protected function getAliasFormat();
56
}
57