1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Liip/FunctionalTestBundle |
7
|
|
|
* |
8
|
|
|
* (c) Lukas Kahwe Smith <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Liip\FunctionalTestBundle\Validator; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
18
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
19
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
21
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
22
|
|
|
use Symfony\Component\Validator\Mapping\MetadataInterface; |
23
|
|
|
use Symfony\Component\Validator\Validator\ContextualValidatorInterface; |
24
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
25
|
|
|
|
26
|
|
|
class DataCollectingValidator implements ValidatorInterface, EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ValidatorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $wrappedValidator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ConstraintViolationListInterface |
35
|
|
|
*/ |
36
|
|
|
protected $lastErrors; |
37
|
|
|
|
38
|
|
|
public function __construct(ValidatorInterface $wrappedValidator) |
39
|
|
|
{ |
40
|
|
|
$this->wrappedValidator = $wrappedValidator; |
41
|
|
|
$this->clearLastErrors(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function clearLastErrors(): void |
45
|
|
|
{ |
46
|
|
|
$this->lastErrors = new ConstraintViolationList(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getLastErrors(): ConstraintViolationListInterface |
50
|
|
|
{ |
51
|
|
|
return $this->lastErrors; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getMetadataFor($value): MetadataInterface |
55
|
|
|
{ |
56
|
|
|
return $this->wrappedValidator->getMetadataFor($value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function hasMetadataFor($value): bool |
60
|
|
|
{ |
61
|
|
|
return $this->wrappedValidator->hasMetadataFor($value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function validate($value, $constraints = null, $groups = null): ConstraintViolationListInterface |
65
|
|
|
{ |
66
|
|
|
return $this->lastErrors = $this->wrappedValidator->validate($value, $constraints, $groups); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function validateProperty($object, $propertyName, $groups = null): ConstraintViolationListInterface |
70
|
|
|
{ |
71
|
|
|
return $this->wrappedValidator->validateProperty($object, $propertyName, $groups); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null): ConstraintViolationListInterface |
75
|
|
|
{ |
76
|
|
|
return $this->wrappedValidator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function startContext(): ContextualValidatorInterface |
80
|
|
|
{ |
81
|
|
|
return $this->wrappedValidator->startContext(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface |
85
|
|
|
{ |
86
|
|
|
return $this->wrappedValidator->inContext($context); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function onKernelRequest(GetResponseEvent $event): void |
90
|
|
|
{ |
91
|
|
|
// The isMainRequest method has been added in Symfony 5.3 |
92
|
|
|
if (method_exists($event, 'isMainRequest')) { |
93
|
|
|
if ($event->isMainRequest()) { |
94
|
|
|
$this->clearLastErrors(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// For Symfony < 5.3, call the legacy method |
101
|
|
|
if ($event->isMasterRequest()) { |
|
|
|
|
102
|
|
|
$this->clearLastErrors(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public static function getSubscribedEvents(): array |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
1 |
|
KernelEvents::REQUEST => ['onKernelRequest', 99999], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.