-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.php
More file actions
119 lines (90 loc) · 3.32 KB
/
Copy pathValidator.php
File metadata and controls
119 lines (90 loc) · 3.32 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
<?php
namespace ProgrammatorDev\FluentValidator;
use ProgrammatorDev\FluentValidator\Exception\ValidationFailedException;
use ProgrammatorDev\FluentValidator\Factory\ConstraintFactory;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Contracts\Translation\TranslatorInterface;
/** @mixin StaticValidatorInterface */
class Validator
{
/** @var Constraint[] */
private array $constraints = [];
/** @var string[] */
private static array $namespaces = [];
private static ?TranslatorInterface $translator = null;
private static function create(): self
{
return new self();
}
public static function __callStatic(string $constraintName, array $arguments = []): self
{
return self::create()->__call($constraintName, $arguments);
}
public function __call(string $constraintName, array $arguments = []): self
{
$constraintFactory = new ConstraintFactory();
foreach (self::$namespaces as $namespace) {
$constraintFactory->addNamespace($namespace);
}
$constraint = $constraintFactory->create($constraintName, $arguments);
$this->addConstraint($constraint);
return $this;
}
public function validate(mixed $value, ?string $name = null, array|null|string|GroupSequence $groups = null): ConstraintViolationListInterface
{
$builder = Validation::createValidatorBuilder();
if (self::$translator !== null) {
$builder->setTranslator(self::$translator);
}
$context = $builder->getValidator()->startContext();
if ($name !== null) {
$context->atPath($name);
}
$context->validate($value, $this->constraints, $groups);
return $context->getViolations();
}
public function assert(mixed $value, ?string $name = null, array|null|string|GroupSequence $groups = null): void
{
$violations = $this->validate($value, $name, $groups);
if ($violations->count() > 0) {
$violation = $violations->get(0);
$message = $violation->getMessage();
if ($name !== null) {
$message = sprintf('%s: %s', $violation->getPropertyPath(), $message);
}
throw new ValidationFailedException($message, $value, $violations);
}
}
public function isValid(mixed $value, array|null|string|GroupSequence $groups = null): bool
{
$violations = $this->validate($value, groups: $groups);
return $violations->count() === 0;
}
public function toArray(): array
{
return $this->constraints;
}
private function addConstraint(Constraint $constraint): void
{
$this->constraints[] = $constraint;
}
public static function addNamespace(string $namespace): void
{
if (in_array($namespace, self::$namespaces, true)) {
return;
}
self::$namespaces[] = $namespace;
}
public static function setTranslator(?TranslatorInterface $translator): void
{
self::$translator = $translator;
}
public static function reset(): void
{
self::$namespaces = [];
self::$translator = null;
}
}