Passed
Push — master ( 439fe5...0de0cb )
by
unknown
01:42
created

Field::isValid()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
cc 5
eloc 11
nc 8
nop 2
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 8.439
1
<?php
2
3
namespace PluginSimpleValidate;
4
5
use PluginSimpleValidate\Libraries\Language;
6
7
class Field implements \PluginSimpleValidate\Contracts\Field
8
{
9
    /**
10
     * @var string
11
     */
12
    private $name;
13
14
    /**
15
     * @var string
16
     */
17
    private $value;
18
19
    /**
20
     * @var array
21
     */
22
    private $errors;
23
24
    /**
25
     * @var array
26
     * array of Rule
27
     */
28
    private $rules = [];
29
30
    /**
31
     * @var bool
32
     */
33
    private $status;
34
35
    /**
36
     * Field constructor.
37
     * @param string $name
38
     * @param mixed $value
39
     */
40 4
    public function __construct(string $name, $value)
41
    {
42 4
        $this->name = $name;
43 4
        $this->value = $value;
44 4
        $this->errors = [];
45 4
    }
46
47
    /**
48
     * @param string $rulesMethod
49
     * @return $this
50
     */
51 3
    public function addRules(string $rulesMethod)
52
    {
53 3
        $this->rules[$rulesMethod] = RuleMapping::getRule($rulesMethod);
54 3
        return $this;
55
    }
56
57
    /**
58
     * @param Language $language
59
     * @param bool $break_when_error
60
     * @return bool
61
     */
62 3
    public function isValid(Language $language, $break_when_error = false) : bool
63
    {
64
        /** @var \PluginSimpleValidate\Contracts\Rule $rule */
65 3
        foreach ($this->rules as $ruleName => $rule) {
66 3
            if (!$rule->isValid($language, $this->value)) {
67 3
                $this->status = false;
68 3
                $this->errors[] = $language->getTranslation(
69 3
                    $rule->getLangKey()
70
                );
71
72
                /**
73
                 * break when there is any rule error
74
                 */
75 3
                if ($break_when_error === true) {
76 3
                    break;
77
                }
78
            }
79
        }
80
81 3
        if (empty($this->errors)) {
82
            $this->status = true;
83
        }
84
85 3
        return $this->status;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 2
    public function getName(): string
92
    {
93 2
        return $this->name;
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99 1
    public function getValue()
100
    {
101 1
        return $this->value;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function getRules(): array
108
    {
109
        return $this->rules;
110
    }
111
112
    /**
113
     * @return array
114
     */
115 3
    public function getErrors(): array
116
    {
117 3
        return $this->errors;
118
    }
119
120
    /**
121
     * @return $this
122
     */
123 3
    public function required()
124
    {
125 3
        $this->addRules('required');
126 3
        return $this;
127
    }
128
129
    /**
130
     * @return $this
131
     */
132 2
    public function validEmail()
133
    {
134 2
        $this->addRules('valid_email');
135 2
        return $this;
136
    }
137
}