|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hop\Validator\Strategy; |
|
6
|
|
|
|
|
7
|
|
|
use Hop\Validator\Strategy\StructureField\ConditionalStrategy; |
|
8
|
|
|
|
|
9
|
|
|
final class StructureField implements FieldInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $fieldName; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
private $required; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var callable|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $condition; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private $isArray; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Strategy|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private $strategy; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ConditionalStrategy[] |
|
38
|
|
|
*/ |
|
39
|
|
|
private $conditionalStrategies = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* StructureField constructor. |
|
43
|
|
|
* @param string $fieldName |
|
44
|
|
|
* @param bool $required |
|
45
|
|
|
* @param callable|null $condition |
|
46
|
|
|
* @param bool $isArray |
|
47
|
|
|
* @param Strategy|null $strategy |
|
48
|
|
|
*/ |
|
49
|
9 |
|
public function __construct( |
|
50
|
|
|
string $fieldName, |
|
51
|
|
|
bool $required, |
|
52
|
|
|
?callable $condition, |
|
53
|
|
|
bool $isArray, |
|
54
|
|
|
?Strategy $strategy |
|
55
|
|
|
) { |
|
56
|
9 |
|
$this->fieldName = $fieldName; |
|
57
|
9 |
|
$this->required = $required; |
|
58
|
9 |
|
$this->condition = $condition; |
|
59
|
9 |
|
$this->isArray = $isArray; |
|
60
|
9 |
|
$this->strategy = $strategy; |
|
61
|
9 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function fieldName(): string |
|
67
|
|
|
{ |
|
68
|
4 |
|
return $this->fieldName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function required(): bool |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->required; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function condition(): ?callable |
|
83
|
|
|
{ |
|
84
|
3 |
|
return $this->condition; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
5 |
|
public function isArray(): bool |
|
91
|
|
|
{ |
|
92
|
5 |
|
return $this->isArray; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param ConditionalStrategy $strategy |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function registerConditionalStrategy(ConditionalStrategy $strategy): void |
|
99
|
|
|
{ |
|
100
|
2 |
|
if ($this->strategy !== null) { |
|
101
|
|
|
throw new \RuntimeException('Main strategy is set already'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
$this->conditionalStrategies[] = $strategy; |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param mixed $data |
|
109
|
|
|
* @return Strategy |
|
110
|
|
|
*/ |
|
111
|
8 |
|
public function strategy($data): Strategy |
|
112
|
|
|
{ |
|
113
|
8 |
|
if ($this->strategy !== null) { |
|
114
|
5 |
|
return $this->strategy; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
if (\count($this->conditionalStrategies) === 0) { |
|
118
|
1 |
|
throw new \RuntimeException('Main strategy is not set and there is no conditional strategies added'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
foreach ($this->conditionalStrategies as $strategy) { |
|
122
|
2 |
|
if ($strategy->shouldBeApplied($data)) { |
|
123
|
2 |
|
return $strategy->strategy(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
throw new \RuntimeException('No strategy has been applied to the data'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|