Completed
Push — master ( e4e8e1...017c51 )
by Alexis
02:26
created

ValidatorExtension::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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'] = !empty($functionsNames['error']) ? $functionsNames['error'] : 'error';
40
        $this->functionsNames['errors'] = !empty($functionsNames['errors']) ? $functionsNames['errors'] : 'errors';
41
        $this->functionsNames['rule_error'] = !empty($functionsNames['rule_error']) ? $functionsNames['rule_error'] : 'rule_error';
42
        $this->functionsNames['has_error'] = !empty($functionsNames['has_error']) ? $functionsNames['has_error'] : 'has_error';
43
        $this->functionsNames['has_errors'] = !empty($functionsNames['has_errors']) ? $functionsNames['has_errors'] : 'has_errors';
44
        $this->functionsNames['val'] = !empty($functionsNames['val']) ? $functionsNames['val'] : 'val';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getFunctions()
51
    {
52
        return [
53
            new TwigFunction($this->functionsNames['error'], [$this, 'getError']),
54
            new TwigFunction($this->functionsNames['errors'], [$this, 'getErrors']),
55
            new TwigFunction($this->functionsNames['rule_error'], [$this, 'getRuleError']),
56
            new TwigFunction($this->functionsNames['has_error'], [$this, 'hasError']),
57
            new TwigFunction($this->functionsNames['has_errors'], [$this, 'hasErrors']),
58
            new TwigFunction($this->functionsNames['val'], [$this, 'getValue'])
59
        ];
60
    }
61
62
    /**
63
     * Gets the first validation error of a parameter.
64
     *
65
     * @param string $param
66
     *
67
     * @return string
68
     */
69
    public function getError($param)
70
    {
71
        return $this->validator->getFirstError($param);
72
    }
73
74
    /**
75
     * Gets the validation errors of a parameter.
76
     *
77
     * @param string $param
78
     *
79
     * @return array
80
     */
81
    public function getErrors($param = '')
82
    {
83
        return $param ? $this->validator->getParamErrors($param) : $this->validator->getErrors();
84
    }
85
86
    /**
87
     * Gets the error of a rule for a parameter.
88
     *
89
     * @param string $param
90
     * @param string $rule
91
     *
92
     * @return string
93
     */
94
    public function getRuleError($param, $rule)
95
    {
96
        return $this->validator->getParamRuleError($param, $rule);
97
    }
98
99
    /**
100
     * Gets the value of a parameter in validated data.
101
     *
102
     * @param string $param
103
     *
104
     * @return string
105
     */
106
    public function getValue($param)
107
    {
108
        return $this->validator->getValue($param);
109
    }
110
111
    /**
112
     * Tells if there are validation errors for a parameter.
113
     *
114
     * @param string $param
115
     *
116
     * @return bool
117
     */
118
    public function hasError($param)
119
    {
120
        return !empty($this->validator->getParamErrors($param));
121
    }
122
123
    /**
124
     * Tells if there are validation errors.
125
     *
126
     * @return bool
127
     */
128
    public function hasErrors()
129
    {
130
        return !$this->validator->isValid();
131
    }
132
}
133