AbstractValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 65
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
process() 0 1 ?
A __construct() 0 4 1
A validate() 0 12 2
A getField() 0 4 1
A getMessage() 0 5 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
}