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