Passed
Push — master ( 873950...1e5375 )
by y
01:29
created

Choice::when()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Helix\DB\SQL;
4
5
use Helix\DB;
6
7
/**
8
 * Represents a `CASE` expression, with or without a *subject* expression.
9
 *
10
 * If a subject is used, `WHEN` relies on equating the subject's evaluation to a literal value.
11
 * This is also known as a "simple case", and is not null-safe.
12
 * If you are choosing between known states, this may be the use-case for you.
13
 *
14
 * Otherwise `WHEN` relies on arbitrary predication.
15
 * This is also known as a "searched case", and may be constructed in a null-safe manner.
16
 * If you are choosing amongst complex states, this may be the use-case for you.
17
 *
18
 * With a subject (simple case):
19
 *
20
 * ```
21
 * CASE subject
22
 *  WHEN value THEN value
23
 *  ...
24
 * END
25
 * ```
26
 *
27
 * Without a subject (searched case):
28
 *
29
 * ```
30
 * CASE
31
 *  WHEN predicate THEN value
32
 *  ...
33
 * END
34
 * ```
35
 */
36
class Choice extends Value {
37
38
    /**
39
     * @var string
40
     */
41
    protected $else;
42
43
    /**
44
     * @var string
45
     */
46
    protected $subject;
47
48
    /**
49
     * [when => then]
50
     *
51
     * @var string[]
52
     */
53
    protected $values = [];
54
55
    /**
56
     * @param DB $db
57
     * @param string $subject
58
     * @param array $values `[when => then]` for the subject.
59
     */
60
    public function __construct (DB $db, string $subject = null, array $values = []) {
61
        parent::__construct($db, '');
62
        $this->subject = $subject;
63
        $this->whenValues($values);
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function __toString () {
70
        $sql = 'CASE';
71
        if (isset($this->subject)) {
72
            $sql .= " {$this->subject}";
73
        }
74
        foreach ($this->values as $when => $then) {
75
            $sql .= " WHEN {$when} THEN {$then}";
76
        }
77
        if (isset($this->else)) {
78
            $sql .= " ELSE {$this->else}";
79
        }
80
        $sql .= ' END';
81
        return $sql;
82
    }
83
84
    /**
85
     * @param string|ValueInterface $else
86
     * @return $this
87
     */
88
    public function else ($else) {
89
        $this->else = isset($else) ? $this->db->quote($else) : null;
90
        return $this;
91
    }
92
93
    /**
94
     * Quotes and sets a conditional value.
95
     *
96
     * @param string|ValueInterface $expression
97
     * @param string|ValueInterface $then
98
     * @return $this
99
     */
100
    public function when ($expression, $then) {
101
        $this->values[$this->db->quote($expression)] = $this->db->quote($then);
102
        return $this;
103
    }
104
105
    /**
106
     * Quotes and sets multiple conditional values for the subject.
107
     *
108
     * > :warning: Keys are quoted as literal values.
109
     * > Use {@link when()} if you need an expression for the `WHEN` clause.
110
     *
111
     * @param array $values `[when => then]`
112
     * @return $this
113
     */
114
    public function whenValues (array $values) {
115
        foreach ($values as $when => $then) {
116
            $this->when($when, $then);
117
        }
118
        return $this;
119
    }
120
}