|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Bootloader\Security; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
15
|
|
|
use Spiral\Bootloader\TokenizerBootloader; |
|
16
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
17
|
|
|
use Spiral\Config\Patch\Append; |
|
18
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
19
|
|
|
use Spiral\Security\RulesInterface; |
|
20
|
|
|
use Spiral\Validation\Checker; |
|
21
|
|
|
use Spiral\Validation\Condition; |
|
22
|
|
|
use Spiral\Validation\ParserInterface; |
|
23
|
|
|
use Spiral\Validation\RuleParser; |
|
24
|
|
|
use Spiral\Validation\ValidationInterface; |
|
25
|
|
|
use Spiral\Validation\ValidationProvider; |
|
26
|
|
|
use Spiral\Validation\ValidatorInterface; |
|
27
|
|
|
|
|
28
|
|
|
final class ValidationBootloader extends Bootloader implements SingletonInterface |
|
29
|
|
|
{ |
|
30
|
|
|
protected const DEPENDENCIES = [ |
|
31
|
|
|
TokenizerBootloader::class, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
protected const SINGLETONS = [ |
|
35
|
|
|
ValidationInterface::class => ValidationProvider::class, |
|
36
|
|
|
RulesInterface::class => ValidationProvider::class, |
|
37
|
|
|
ParserInterface::class => RuleParser::class, |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ConfiguratorInterface */ |
|
41
|
|
|
private $config; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ConfiguratorInterface $config |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(ConfiguratorInterface $config) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param TokenizerBootloader $tokenizer |
|
53
|
|
|
*/ |
|
54
|
|
|
public function boot(TokenizerBootloader $tokenizer): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->config->setDefaults( |
|
57
|
|
|
'validation', |
|
58
|
|
|
[ |
|
59
|
|
|
// Checkers are resolved using container and provide ability to isolate some validation rules |
|
60
|
|
|
// under common name and class. You can register new checkers at any moment without any |
|
61
|
|
|
// performance issues. |
|
62
|
|
|
'checkers' => [ |
|
63
|
|
|
'type' => Checker\TypeChecker::class, |
|
64
|
|
|
'number' => Checker\NumberChecker::class, |
|
65
|
|
|
'mixed' => Checker\MixedChecker::class, |
|
66
|
|
|
'address' => Checker\AddressChecker::class, |
|
67
|
|
|
'string' => Checker\StringChecker::class, |
|
68
|
|
|
'file' => Checker\FileChecker::class, |
|
69
|
|
|
'image' => Checker\ImageChecker::class, |
|
70
|
|
|
'datetime' => Checker\DatetimeChecker::class, |
|
71
|
|
|
'entity' => Checker\EntityChecker::class, |
|
72
|
|
|
], |
|
73
|
|
|
|
|
74
|
|
|
// Enable/disable validation conditions |
|
75
|
|
|
'conditions' => [ |
|
76
|
|
|
'absent' => Condition\AbsentCondition::class, |
|
77
|
|
|
'present' => Condition\PresentCondition::class, |
|
78
|
|
|
'anyOf' => Condition\AnyOfCondition::class, |
|
79
|
|
|
'noneOf' => Condition\NoneOfCondition::class, |
|
80
|
|
|
'withAny' => Condition\WithAnyCondition::class, |
|
81
|
|
|
'withoutAny' => Condition\WithoutAnyCondition::class, |
|
82
|
|
|
'withAll' => Condition\WithAllCondition::class, |
|
83
|
|
|
'withoutAll' => Condition\WithoutAllCondition::class, |
|
84
|
|
|
], |
|
85
|
|
|
|
|
86
|
|
|
// Aliases are only used to simplify developer life. |
|
87
|
|
|
'aliases' => [ |
|
88
|
|
|
'notEmpty' => 'type::notEmpty', |
|
89
|
|
|
'notNull' => 'type::notNull', |
|
90
|
|
|
'required' => 'type::notEmpty', |
|
91
|
|
|
'datetime' => 'type::datetime', |
|
92
|
|
|
'timezone' => 'type::timezone', |
|
93
|
|
|
'bool' => 'type::boolean', |
|
94
|
|
|
'boolean' => 'type::boolean', |
|
95
|
|
|
'arrayOf' => 'type::arrayOf', |
|
96
|
|
|
'cardNumber' => 'mixed::cardNumber', |
|
97
|
|
|
'regexp' => 'string::regexp', |
|
98
|
|
|
'email' => 'address::email', |
|
99
|
|
|
'url' => 'address::url', |
|
100
|
|
|
'file' => 'file::exists', |
|
101
|
|
|
'uploaded' => 'file::uploaded', |
|
102
|
|
|
'filesize' => 'file::size', |
|
103
|
|
|
'image' => 'image::valid', |
|
104
|
|
|
'array' => 'is_array', |
|
105
|
|
|
'callable' => 'is_callable', |
|
106
|
|
|
'double' => 'is_double', |
|
107
|
|
|
'float' => 'is_float', |
|
108
|
|
|
'int' => 'is_int', |
|
109
|
|
|
'integer' => 'is_integer', |
|
110
|
|
|
'numeric' => 'is_numeric', |
|
111
|
|
|
'long' => 'is_long', |
|
112
|
|
|
'null' => 'is_null', |
|
113
|
|
|
'object' => 'is_object', |
|
114
|
|
|
'real' => 'is_real', |
|
115
|
|
|
'resource' => 'is_resource', |
|
116
|
|
|
'scalar' => 'is_scalar', |
|
117
|
|
|
'string' => 'is_string', |
|
118
|
|
|
'match' => 'mixed::match', |
|
119
|
|
|
], |
|
120
|
|
|
] |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$tokenizer->addDirectory(\dirname( |
|
125
|
|
|
(new \ReflectionClass(ValidatorInterface::class)) |
|
126
|
|
|
->getFileName() |
|
127
|
|
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $alias |
|
132
|
|
|
* @param mixed $checker |
|
133
|
|
|
*/ |
|
134
|
|
|
public function addChecker(string $alias, $checker): void |
|
135
|
|
|
{ |
|
136
|
|
|
$this->config->modify('validation', new Append('checkers', $alias, $checker)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $alias |
|
141
|
|
|
* @param mixed $condition |
|
142
|
|
|
*/ |
|
143
|
|
|
public function addCondition(string $alias, $condition): void |
|
144
|
|
|
{ |
|
145
|
|
|
$this->config->modify('validation', new Append('conditions', $alias, $condition)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $alias |
|
150
|
|
|
* @param string $target |
|
151
|
|
|
*/ |
|
152
|
|
|
public function addAlias(string $alias, string $target): void |
|
153
|
|
|
{ |
|
154
|
|
|
$this->config->modify('validation', new Append('aliases', $alias, $target)); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|