Completed
Push — master ( f3cdf4...360009 )
by Beniamin
04:07
created

QueryClauseExpression::getWrappedExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\QueryBuilder\Expression;
13
14
use Phuria\QueryBuilder\ExprBuilder;
15
16
/**
17
 * @author Beniamin Jonatan Šimko <[email protected]>
18
 */
19
class QueryClauseExpression implements ExpressionInterface
20
{
21
    const CLAUSE_WHERE = 'WHERE';
22
    const CLAUSE_SELECT = 'SELECT';
23
    const CLAUSE_ORDER_BY = 'ORDER BY';
24
    const CLAUSE_SET = 'SET';
25
    const CLAUSE_GROUP_BY = 'GROUP BY';
26
    const CLAUSE_HAVING = 'HAVING';
27
28
    /**
29
     * @var string $clause
30
     */
31
    private $clause;
32
33
    /**
34
     * @var ExpressionInterface $wrappedExpression
35
     */
36
    private $wrappedExpression;
37
38
    /**
39
     * @var bool $quietWhenEmpty
40
     */
41
    private $quietWhenEmpty;
42
43
    /**
44
     * @param string              $clause
45
     * @param ExpressionInterface $expression
46
     * @param bool                $quietWhenEmpty
47
     */
48 24
    public function __construct($clause, ExpressionInterface $expression, $quietWhenEmpty = true)
49
    {
50 24
        $this->clause = $clause;
51 24
        $this->wrappedExpression = $expression;
52 24
        $this->quietWhenEmpty = $quietWhenEmpty;
53 24
    }
54
55
    /**
56
     * @return boolean
57
     */
58 24
    public function isQuietWhenEmpty()
59
    {
60 24
        return $this->quietWhenEmpty;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 24
    public function isEmpty()
67
    {
68 24
        $expr = $this->wrappedExpression;
69
70 24
        if ($expr instanceof ExprBuilder) {
71
            $expr = $expr->getWrappedExpression();
72
        }
73
74 24
        if ($expr instanceof EmptyExpression) {
75
            return true;
76
        }
77
78 24
        if ($expr instanceof ExpressionCollection && $expr->isEmpty()) {
79 22
            return true;
80
        }
81
82 23
        return false;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 21
    public function getClause()
89
    {
90 21
        return $this->clause;
91
    }
92
93
    /**
94
     * @return ExpressionInterface
95
     */
96 21
    public function getWrappedExpression()
97
    {
98 21
        return $this->wrappedExpression;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 24
    public function compile()
105
    {
106 24
        if ($this->isQuietWhenEmpty() && $this->isEmpty()) {
107 22
            return '';
108 9
        } else if ($this->isEmpty()) {
109
            return $this->clause;
110
        }
111
112 9
        return $this->clause . ' ' . $this->wrappedExpression->compile();
113
    }
114
}