Passed
Push — master ( e9ffd0...78a3b8 )
by y
01:28
created

Branch::else()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\DB\Fluent;
4
5
use Helix\DB;
6
7
/**
8
 * 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
 * @method static static factory(DB $db, string $subject = null, array $values = [])
37
 */
38
class Branch extends Value
39
{
40
41
    /**
42
     * @var string
43
     */
44
    protected $else;
45
46
    /**
47
     * @var string
48
     */
49
    protected $subject;
50
51
    /**
52
     * [when => then]
53
     *
54
     * @var string[]
55
     */
56
    protected $values = [];
57
58
    /**
59
     * @param DB $db
60
     * @param null|string $subject
61
     * @param array $values `[when => then]` for the subject.
62
     */
63
    public function __construct(DB $db, string $subject = null, array $values = [])
64
    {
65
        parent::__construct($db, '');
66
        $this->subject = $subject;
67
        $this->whenValues($values);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function __toString(): string
74
    {
75
        $sql = 'CASE';
76
        if (isset($this->subject)) {
77
            $sql .= " {$this->subject}";
78
        }
79
        foreach ($this->values as $when => $then) {
80
            $sql .= " WHEN {$when} THEN {$then}";
81
        }
82
        if (isset($this->else)) {
83
            $sql .= " ELSE {$this->else}";
84
        }
85
        $sql .= ' END';
86
        return $sql;
87
    }
88
89
    /**
90
     * @param string|ValueInterface $else
91
     * @return $this
92
     */
93
    public function else($else)
94
    {
95
        $this->else = isset($else) ? $this->db->quote($else) : null;
96
        return $this;
97
    }
98
99
    /**
100
     * Quotes and sets a conditional value.
101
     *
102
     * @param string|ValueInterface $expression
103
     * @param string|ValueInterface $then
104
     * @return $this
105
     */
106
    public function when($expression, $then)
107
    {
108
        $this->values[$this->db->quote($expression)] = $this->db->quote($then);
109
        return $this;
110
    }
111
112
    /**
113
     * Quotes and sets multiple conditional values for the subject.
114
     *
115
     * > :warning: Keys are quoted as literal values.
116
     * > Use {@link when()} if you need an expression for the `WHEN` clause.
117
     *
118
     * @param array $values `[when => then]`
119
     * @return $this
120
     */
121
    public function whenValues(array $values)
122
    {
123
        foreach ($values as $when => $then) {
124
            $this->when($when, $then);
125
        }
126
        return $this;
127
    }
128
}
129