Repository navigation
Expand file tree
/
Copy pathValidatorTask.php
More file actions
121 lines (103 loc) · 3.79 KB
/
Copy pathValidatorTask.php
File metadata and controls
121 lines (103 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Task\Validation;
use CleverAge\ProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Validator\ConstraintLoader;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Validate the input and pass it to the output.
*
* @final since 5.4
*/
class ValidatorTask extends AbstractConfigurableTask
{
public function __construct(
protected LoggerInterface $logger,
protected ValidatorInterface $validator,
) {
}
public function execute(ProcessState $state): void
{
$options = $this->getOptions($state);
$violations = $this->validator->validate($state->getInput(), $options['constraints'], $options['groups']);
if ($violations->count() > 0) {
/** @var ConstraintViolationInterface $violation */
foreach ($violations as $violation) {
$invalidValue = $violation->getInvalidValue();
$logContext = [
'property' => $violation->getPropertyPath(),
'violation_code' => $violation->getCode(),
'invalid_value' => $invalidValue,
];
if ($options['log_errors']) {
$this->logger->log($options['log_errors'], $violation->getMessage(), $logContext);
}
}
if ($options['error_output_violations']) {
$state->setErrorOutput($violations);
$state->setSkipped(true);
return;
}
throw new UnexpectedValueException("{$violations->count()} constraint violations detected on validation");
}
$state->setOutput($state->getInput());
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('log_errors', LogLevel::CRITICAL);
$resolver->setAllowedValues(
'log_errors',
[
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::DEBUG,
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
true,
false,
]
);
$resolver->setNormalizer(
'log_errors',
static function (Options $options, $value) {
if (true === $value) {
return LogLevel::CRITICAL;
}
return $value;
}
);
$resolver->setDefault('groups', null);
$resolver->setAllowedTypes('groups', ['null', 'array']);
$resolver->setDefault('constraints', null);
$resolver->setAllowedTypes('constraints', ['null', 'array']);
$resolver->setNormalizer(
'constraints',
static function (Options $options, $constraints): ?array {
if (null === $constraints) {
return null;
}
return (new ConstraintLoader())->buildConstraints($constraints);
}
);
$resolver->setDefault('error_output_violations', false);
$resolver->setAllowedTypes('error_output_violations', ['bool']);
}
}