Completed
Push — master ( 017c51...952a61 )
by Alexis
01:34
created

ValidatorExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace Awurth\SlimValidation;
4
5
use Twig\Extension\AbstractExtension;
6
use Twig\TwigFunction;
7
8
/**
9
 * ValidatorExtension.
10
 *
11
 * @author Alexis Wurth <[email protected]>
12
 */
13
class ValidatorExtension extends AbstractExtension
14
{
15
    /**
16
     * Array of names for Twig functions.
17
     *
18
     * @var array
19
     */
20
    protected $functionsNames;
21
22
    /**
23
     * Validator service.
24
     *
25
     * @var Validator
26
     */
27
    protected $validator;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param Validator $validator The validator instance
33
     * @param array $functionsNames An array of names for Twig functions
34
     */
35
    public function __construct(Validator $validator, $functionsNames = [])
36
    {
37
        $this->validator = $validator;
38
39
        $this->functionsNames['error'] = $functionsNames['error'] ?? 'error';
40
        $this->functionsNames['errors'] = $functionsNames['errors'] ?? 'errors';
41
        $this->functionsNames['has_error'] = $functionsNames['has_error'] ?? 'has_error';
42
        $this->functionsNames['has_errors'] = $functionsNames['has_errors'] ?? 'has_errors';
43
        $this->functionsNames['val'] = $functionsNames['val'] ?? 'val';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getFunctions()
50
    {
51
        return [
52
            new TwigFunction($this->functionsNames['error'], [$this, 'getError']),
53
            new TwigFunction($this->functionsNames['errors'], [$this, 'getErrors']),
54
            new TwigFunction($this->functionsNames['has_error'], [$this, 'hasError']),
55
            new TwigFunction($this->functionsNames['has_errors'], [$this, 'hasErrors']),
56
            new TwigFunction($this->functionsNames['val'], [$this, 'getValue'])
57
        ];
58
    }
59
60
    /**
61
     * Gets the first validation error of a parameter.
62
     *
63
     * @param string $param
64
     * @param string $key
65
     * @param string $group
66
     *
67
     * @return string
68
     */
69
    public function getError($param, $key = null, $group = null)
70
    {
71
        return $this->validator->getError($param, $key, $group);
72
    }
73
74
    /**
75
     * Gets the validation errors of a parameter.
76
     *
77
     * @param string $param
78
     * @param string $group
79
     *
80
     * @return string[]
81
     */
82
    public function getErrors($param = null, $group = null)
83
    {
84
        return $this->validator->getErrors($param, $group);
85
    }
86
87
    /**
88
     * Gets the value of a parameter in validated data.
89
     *
90
     * @param string $param
91
     * @param string $group
92
     *
93
     * @return string
94
     */
95
    public function getValue($param, $group = null)
96
    {
97
        return $this->validator->getValue($param, $group);
98
    }
99
100
    /**
101
     * Tells if there are validation errors for a parameter.
102
     *
103
     * @param string $param
104
     * @param string $group
105
     *
106
     * @return bool
107
     */
108
    public function hasError($param, $group = null)
109
    {
110
        return !empty($this->validator->getErrors($param, $group));
111
    }
112
113
    /**
114
     * Tells if there are validation errors.
115
     *
116
     * @return bool
117
     */
118
    public function hasErrors()
119
    {
120
        return !$this->validator->isValid();
121
    }
122
}
123