-
Notifications
You must be signed in to change notification settings - Fork 23
/
ValidatorExtension.php
77 lines (65 loc) · 2.25 KB
/
ValidatorExtension.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the Awurth Validator package.
*
* (c) Alexis Wurth <awurth.dev@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Awurth\Validator\Twig;
use Awurth\Validator\StatefulValidatorInterface;
use Awurth\Validator\ValidationFailureCollectionInterface;
use Awurth\Validator\ValidationFailureInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Validator Twig Extension.
*
* @author Alexis Wurth <awurth.dev@gmail.com>
*/
final class ValidatorExtension extends AbstractExtension
{
/**
* An array of names for Twig functions.
*
* @var string[]
*/
protected array $functionNames;
/**
* @param string[] $functionNames An array of names for Twig functions
*/
public function __construct(private readonly StatefulValidatorInterface $validator, array $functionNames = [])
{
$this->functionNames['error'] = $functionNames['error'] ?? 'error';
$this->functionNames['errors'] = $functionNames['errors'] ?? 'errors';
$this->functionNames['has_errors'] = $functionNames['has_errors'] ?? 'has_errors';
}
public function getFunctions(): array
{
return [
new TwigFunction($this->functionNames['error'], [$this, 'findFirst']),
new TwigFunction($this->functionNames['errors'], [$this, 'findErrors']),
new TwigFunction($this->functionNames['has_errors'], [$this, 'hasErrors']),
];
}
public function findFirst(?callable $callback = null): ?ValidationFailureInterface
{
if (null === $callback) {
return $this->validator->getFailures()->has(0) ? $this->validator->getFailures()->get(0) : null;
}
return $this->validator->getFailures()->find($callback);
}
public function findErrors(?callable $callback = null): ValidationFailureCollectionInterface
{
if (null === $callback) {
return $this->validator->getFailures();
}
return $this->validator->getFailures()->filter($callback);
}
public function hasErrors(): bool
{
return $this->validator->getFailures()->count() !== 0;
}
}