1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Sql\Statement; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\GroupedSubQuery; |
21
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
22
|
|
|
use ArekX\PQL\Query; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Represents a CASE statement. |
26
|
|
|
*/ |
27
|
|
|
class CaseWhen extends Query implements GroupedSubQuery |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Of value to be set for the CASE |
31
|
|
|
* |
32
|
|
|
* @param array|StructuredQuery|null $of |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
2 |
|
public function of(StructuredQuery|array|null $of): static |
36
|
|
|
{ |
37
|
2 |
|
$this->use('of', $of); |
38
|
2 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* When cases to be set |
43
|
|
|
* |
44
|
|
|
* @param array|StructuredQuery $when |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
2 |
|
public function when(array|StructuredQuery $when): static |
48
|
|
|
{ |
49
|
2 |
|
$this->use('when', $when); |
50
|
2 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Appends another WHEN case |
55
|
|
|
* |
56
|
|
|
* @param array|StructuredQuery $when Condition |
57
|
|
|
* @param array|StructuredQuery $then Result |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
3 |
|
public function addWhen(StructuredQuery|array $when, StructuredQuery|array $then): static |
61
|
|
|
{ |
62
|
3 |
|
$this->append('when', [$when, $then]); |
63
|
3 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds else for default case |
68
|
|
|
* |
69
|
|
|
* @param array|StructuredQuery $else Else result. |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
2 |
|
public function else(StructuredQuery|array $else): static |
73
|
|
|
{ |
74
|
2 |
|
$this->use('else', $else); |
75
|
2 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|