Test Failed
Push — develop ( 833877...64b6b0 )
by nguereza
02:36
created

AbstractValidator::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Platine\Framework\Demo\Form\Validator;
4
5
use Platine\Validator\Validator;
6
7
abstract class AbstractValidator
8
{
9
    /**
10
     * The validator instance
11
     * @var Validator
12
     */
13
    protected Validator $validator;
14
15
    /**
16
     * The data to validate
17
     * @var array<string, mixed>
18
     */
19
    protected array $data = [];
20
21
    /**
22
     * Create new instance
23
     * @param Validator|null $validator
24
     */
25
    public function __construct(?Validator $validator = null)
26
    {
27
        $this->validator = $validator ?? new Validator();
28
    }
29
30
31
    public function validate(): bool
32
    {
33
        $this->setRules();
34
        $this->setData();
35
        $this->validator->setData($this->data);
36
37
        return $this->validator->validate();
38
    }
39
40
    public function isValid(): bool
41
    {
42
        return $this->validator->isValid();
43
    }
44
45
    /**
46
     * Return the validations errors
47
     * @return array<string, string>
48
     */
49
    public function getErrors(): array
50
    {
51
        return $this->validator->getErrors();
52
    }
53
54
    /**
55
     *
56
     * @param string $name
57
     * @param mixed $value
58
     * @return $this
59
     */
60
    public function addData(string $name, $value): self
61
    {
62
63
        $this->data[$name] = $value;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Set the validation rules
70
     * @return void
71
     */
72
    abstract public function setRules(): void;
73
74
    /**
75
     * Set the validation data
76
     * @return void
77
     */
78
    abstract public function setData(): void;
79
}
80