1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Rules\Generic; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Blocks\IfBlock; |
22
|
|
|
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface; |
23
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
24
|
|
|
use Limoncello\Validation\Rules\BaseRule; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Limoncello\Validation |
28
|
|
|
*/ |
29
|
|
|
final class IfOperator extends BaseRule |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var callable |
33
|
|
|
*/ |
34
|
|
|
private $condition; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RuleInterface|null |
38
|
|
|
*/ |
39
|
|
|
private $onTrue; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var RuleInterface|null |
43
|
|
|
*/ |
44
|
|
|
private $onFalse; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $settings; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param callable $condition |
53
|
|
|
* @param RuleInterface|null $onTrue |
54
|
|
|
* @param RuleInterface|null $onFalse |
55
|
|
|
* @param array $settings |
56
|
|
|
*/ |
57
|
10 |
|
public function __construct( |
58
|
|
|
callable $condition, |
59
|
|
|
RuleInterface $onTrue = null, |
60
|
|
|
RuleInterface $onFalse = null, |
61
|
|
|
array $settings = [] |
62
|
|
|
) { |
63
|
10 |
|
$this->onTrue = $onTrue; |
64
|
10 |
|
$this->onFalse = $onFalse; |
65
|
10 |
|
$this->condition = $condition; |
66
|
10 |
|
$this->settings = $settings; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
10 |
|
public function toBlock(): ExecutionBlockInterface |
73
|
|
|
{ |
74
|
10 |
|
$onTrue = $this->getOnTrue() === null ? new Success() : $this->getOnTrue(); |
75
|
10 |
|
$onFalse = $this->getOnFalse() === null ? new Success() : $this->getOnFalse(); |
76
|
|
|
|
77
|
10 |
|
return new IfBlock( |
78
|
10 |
|
$this->getCondition(), |
79
|
10 |
|
$onTrue->setParent($this)->toBlock(), |
80
|
10 |
|
$onFalse->setParent($this)->toBlock(), |
81
|
10 |
|
$this->getStandardProperties() + $this->getSettings() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return RuleInterface|null |
87
|
|
|
*/ |
88
|
10 |
|
public function getOnTrue() |
89
|
|
|
{ |
90
|
10 |
|
return $this->onTrue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return RuleInterface|null |
95
|
|
|
*/ |
96
|
10 |
|
public function getOnFalse() |
97
|
|
|
{ |
98
|
10 |
|
return $this->onFalse; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return callable |
103
|
|
|
*/ |
104
|
10 |
|
public function getCondition(): callable |
105
|
|
|
{ |
106
|
10 |
|
return $this->condition; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
10 |
|
public function getSettings(): array |
113
|
|
|
{ |
114
|
10 |
|
return $this->settings; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|