1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Validation\Bootloader; |
6
|
|
|
|
7
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
8
|
|
|
use Spiral\Config\ConfiguratorInterface; |
9
|
|
|
use Spiral\Config\Patch\Set; |
10
|
|
|
use Spiral\Core\Container\SingletonInterface; |
11
|
|
|
use Spiral\Validation\Config\ValidationConfig; |
12
|
|
|
use Spiral\Validation\Exception\ValidationException; |
13
|
|
|
use Spiral\Validation\ValidationInterface; |
14
|
|
|
use Spiral\Validation\ValidationProvider; |
15
|
|
|
use Spiral\Validation\ValidationProviderInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @template TFilterDefinition |
19
|
|
|
*/ |
20
|
|
|
final class ValidationBootloader extends Bootloader implements SingletonInterface |
21
|
|
|
{ |
22
|
|
|
protected const SINGLETONS = [ |
23
|
|
|
ValidationProviderInterface::class => ValidationProvider::class, |
24
|
|
|
ValidationInterface::class => [self::class, 'initDefaultValidator'] |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
private readonly ConfiguratorInterface $config |
29
|
|
|
) { |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function init(): void |
33
|
|
|
{ |
34
|
|
|
$this->config->setDefaults(ValidationConfig::CONFIG, [ |
35
|
|
|
'defaultValidator' => null, |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param class-string<TFilterDefinition> $name |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public function setDefaultValidator(string $name): void |
43
|
|
|
{ |
44
|
|
|
if ($this->config->getConfig(ValidationConfig::CONFIG)['defaultValidator'] === null) { |
45
|
|
|
$this->config->modify(ValidationConfig::CONFIG, new Set('defaultValidator', $name)); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @noRector RemoveUnusedPrivateMethodRector |
51
|
|
|
*/ |
52
|
|
|
private function initDefaultValidator( |
53
|
|
|
ValidationConfig $config, |
54
|
|
|
ValidationProviderInterface $provider |
55
|
|
|
): ValidationInterface { |
56
|
|
|
if ($config->getDefaultValidator() === null) { |
57
|
|
|
throw new ValidationException('Default Validator is not configured.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $provider->getValidation($config->getDefaultValidator()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|