DatabaseExpression::byCondition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Charcoal\Source\Database;
4
5
use UnexpectedValueException;
6
7
// From 'charcoal-core'
8
use Charcoal\Source\Database\DatabaseExpressionInterface;
9
use Charcoal\Source\Expression;
10
11
/**
12
 * The DatabaseFilter makes a Filter SQL-aware.
13
 */
14
class DatabaseExpression extends Expression implements
15
    DatabaseExpressionInterface
16
{
17
    /**
18
     * Retrieve the Filter's SQL as a string to append to a WHERE clause.
19
     *
20
     * @return string
21
     */
22
    public function sql()
23
    {
24
        if ($this->active() && $this->hasCondition()) {
25
            return $this->byCondition();
26
        }
27
28
        return '';
29
    }
30
31
    /**
32
     * Retrieve the custom expression.
33
     *
34
     * @throws UnexpectedValueException If the custom expression is empty.
35
     * @return string
36
     */
37
    protected function byCondition()
38
    {
39
        if (!$this->hasCondition()) {
40
            throw new UnexpectedValueException(
41
                'Custom expression can not be empty.'
42
            );
43
        }
44
45
        return $this->condition();
46
    }
47
}
48