Passed
Push — master ( 222f48...0fa176 )
by y
01:47
created

Choice::__toString()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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