AbstractValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Class AbstractValidator | AbstractValidator.php
4
 * @package Faulancer\Form\Validator
5
 * @author Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\Form\Validator;
8
9
use Faulancer\Form\Type\AbstractType;
10
use Faulancer\Translate\Translator;
11
12
/**
13
 * Class AbstractValidator
14
 */
15
abstract class AbstractValidator
16
{
17
18
    /** @var AbstractType */
19
    protected $field;
20
21
    /**
22
     * @var string
23
     */
24
    protected $errorMessage = '';
25
26
    /**
27
     * AbstractValidator constructor.
28
     * @param AbstractType $field
29
     */
30
    public function __construct(AbstractType $field)
31
    {
32
        $this->field = $field;
33
    }
34
35
    /**
36
     * @return boolean
37
     * @codeCoverageIgnore
38
     */
39
    public function validate()
40
    {
41
        if (!$this->process($this->field->getValue())) {
42
43
            $this->field->addAttribute('class',  'error');
44
            $this->field->setErrorMessages([$this->getMessage()]);
45
            return false;
46
47
        }
48
49
        return true;
50
    }
51
52
    /**
53
     * @return AbstractType
54
     * @codeCoverageIgnore
55
     */
56
    public function getField()
57
    {
58
        return $this->field;
59
    }
60
61
    /**
62
     * Return the error message
63
     * @return string
64
     * @codeCoverageIgnore
65
     */
66
    public function getMessage()
67
    {
68
        $translate = new Translator();
69
        return $translate->translate($this->errorMessage);
70
    }
71
72
    /**
73
     * Init method which must be implemented by every validator
74
     * @param mixed $data
75
     * @return mixed
76
     */
77
    abstract public function process($data);
78
79
}