Completed
Push — master ( 00b18d...cd26d4 )
by BENOIT
03:31
created

CaseHelper::else()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\Where\Helper;
4
5
use BenTools\Where\Expression\Expression;
6
use function BenTools\Where\where;
7
8
final class CaseHelper extends Expression
9
{
10
11
    /**
12
     * @var Expression
13
     */
14
    private $field;
15
16
    /**
17
     * @var Expression[]
18
     */
19
    private $when = [];
20
21
    /**
22
     * @var Expression[]
23
     */
24
    private $then = [];
25
26
    /**
27
     * @var Expression
28
     */
29
    private $else;
30
31
    private $lock = false;
32
33
    /**
34
     * CaseHelper constructor.
35
     * @param Expression|null $field
36
     */
37
    private function __construct(Expression $field = null)
38
    {
39
        $this->field = $field;
40
    }
41
42
    /**
43
     * @param       $expression
44
     * @param array ...$values
45
     * @return CaseHelper
46
     * @throws \InvalidArgumentException
47
     */
48
    public function when($expression, ...$values): self
49
    {
50
        if (count($this->when) !== count($this->then)) {
51
            throw new \RuntimeException("Mising a 'then' somewhere.");
52
        }
53
        if (true === $this->lock) {
54
            throw new \RuntimeException('This conditionnal structure is locked.');
55
        }
56
        $clone = clone $this;
57
        $clone->when[] = where($expression, ...$values);
58
        return $clone;
59
    }
60
61
    /**
62
     * @param       $expression
63
     * @param array ...$values
64
     * @return CaseHelper
65
     * @throws \InvalidArgumentException
66
     */
67
    public function then($expression, ...$values): self
68
    {
69
        if (count($this->when) !== count($this->then) + 1) {
70
            throw new \RuntimeException("Mising a 'when' somewhere.");
71
        }
72
        if (true === $this->lock) {
73
            throw new \RuntimeException('This conditionnal structure is locked.');
74
        }
75
        $clone = clone $this;
76
        $clone->then[] = where($expression, ...$values);
77
        return $clone;
78
    }
79
80
    /**
81
     * @param       $expression
82
     * @param array ...$values
83
     * @return CaseHelper
84
     * @throws \InvalidArgumentException
85
     */
86
    public function else($expression, ...$values): self
87
    {
88
        if (true === $this->lock) {
89
            throw new \RuntimeException('This conditionnal structure is locked.');
90
        }
91
        $clone = clone $this;
92
        $clone->else = where($expression, ...$values);
93
        return $clone;
94
    }
95
96
    /**
97
     * Only for fluent purposes.
98
     *
99
     * @return CaseHelper
100
     */
101
    public function end(): self
102
    {
103
        $clone = clone $this;
104
        $clone->lock = true;
105
        return $clone;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function __toString(): string
112
    {
113
        $parts = ['CASE'];
114
115
        if (null !== $this->field) {
116
            $parts[] = $this->field;
117
        }
118
119
        foreach ($this->when as $key => $value) {
120
            $parts[] = 'WHEN';
121
            $parts[] = $this->when[$key];
122
            $parts[] = 'THEN';
123
            $parts[] = $this->then[$key] ?? null;
124
        }
125
126
        if (null !== $this->else) {
127
            $parts[] = 'ELSE';
128
            $parts[] = $this->else;
129
        }
130
131
        $parts[] = 'END';
132
133
        return implode(' ', $parts);
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getValues(): array
140
    {
141
        $values = [[]];
142
143
        if (null !== $this->field) {
144
            $values[] = $this->field->getValues();
145
        }
146
147
        foreach ($this->when as $key => $whenExpression) {
148
            $values[] = $whenExpression->getValues();
149
            $thenExpression = $this->then[$key];
150
            if (null !== $thenExpression) {
151
                $values[] = $thenExpression->getValues();
152
            }
153
        }
154
155
        if (null !== $this->else) {
156
            $values[] = $this->else->getValues();
157
        }
158
159
        return array_merge(...$values);
160
    }
161
162
    /**
163
     * @param       $expression
164
     * @param array ...$values
165
     * @return CaseHelper
166
     * @throws \InvalidArgumentException
167
     */
168
    public static function create($expression = null, ...$values): self
169
    {
170
        if (null !== $expression) {
171
            $expression = where($expression, ...$values);
172
        }
173
        return new self($expression);
174
    }
175
}
176