1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Blocks; |
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\Common\Reflection\CheckCallableTrait; |
22
|
|
|
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface; |
23
|
|
|
use Limoncello\Validation\Contracts\Blocks\IfExpressionInterface; |
24
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
25
|
|
|
use function assert; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Validation |
29
|
|
|
*/ |
30
|
|
|
final class IfBlock implements IfExpressionInterface |
31
|
|
|
{ |
32
|
|
|
use CheckCallableTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callable |
36
|
|
|
*/ |
37
|
|
|
private $condition; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ExecutionBlockInterface |
41
|
|
|
*/ |
42
|
|
|
private $onTrue; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ExecutionBlockInterface |
46
|
|
|
*/ |
47
|
|
|
private $onFalse; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $properties; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param callable $condition |
56
|
|
|
* @param ExecutionBlockInterface $onTrue |
57
|
|
|
* @param ExecutionBlockInterface $onFalse |
58
|
|
|
* @param array $properties |
59
|
|
|
*/ |
60
|
11 |
|
public function __construct( |
61
|
|
|
callable $condition, |
62
|
|
|
ExecutionBlockInterface $onTrue, |
63
|
|
|
ExecutionBlockInterface $onFalse, |
64
|
|
|
array $properties = [] |
65
|
|
|
) { |
66
|
11 |
|
assert($this->checkConditionCallableSignature($condition)); |
67
|
|
|
|
68
|
11 |
|
$this->condition = $condition; |
69
|
11 |
|
$this->onTrue = $onTrue; |
70
|
11 |
|
$this->onFalse = $onFalse; |
71
|
11 |
|
$this->properties = $properties; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
10 |
|
public function getConditionCallable(): callable |
78
|
|
|
{ |
79
|
10 |
|
return $this->condition; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
10 |
|
public function getOnTrue(): ExecutionBlockInterface |
86
|
|
|
{ |
87
|
10 |
|
return $this->onTrue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
10 |
|
public function getOnFalse(): ExecutionBlockInterface |
94
|
|
|
{ |
95
|
10 |
|
return $this->onFalse; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
11 |
|
public function getProperties(): array |
102
|
|
|
{ |
103
|
11 |
|
return $this->properties; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** @noinspection PhpDocMissingThrowsInspection |
107
|
|
|
* @param callable $procedureCallable |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
11 |
|
private function checkConditionCallableSignature(callable $procedureCallable): bool |
112
|
|
|
{ |
113
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
114
|
11 |
|
return static::checkPublicStaticCallable($procedureCallable, [null, ContextInterface::class], 'bool'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|