Passed
Push — master ( f9ae12...289a67 )
by
unknown
02:11
created

Field::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PluginSimpleValidate;
4
5
use const PluginSimpleValidate\helper\Validate\VAR_LIMIT;
6
use const PluginSimpleValidate\helper\Validate\VAR_LOWER_LIMIT;
7
use const PluginSimpleValidate\helper\Validate\VAR_MATCH;
8
use const PluginSimpleValidate\helper\Validate\VAR_UPPER_LIMIT;
9
use PluginSimpleValidate\Libraries\Language;
10
11
class Field extends \PluginSimpleValidate\BaseAbstract\Field implements \PluginSimpleValidate\Contracts\Field
12
{
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var string
20
     */
21
    private $value;
22
23
    /**
24
     * @var array
25
     */
26
    private $errors;
27
28
    /**
29
     * @var array
30
     * array of Rule
31
     */
32
    private $rules = [];
33
34
    /**
35
     * @var bool
36
     */
37
    private $status;
38
39
    /**
40
     * Field constructor.
41
     * @param string $name
42
     * @param mixed $value
43
     */
44 7
    public function __construct(string $name, $value)
45
    {
46 7
        $this->name = $name;
47 7
        $this->value = $value;
48 7
        $this->errors = [];
49 7
    }
50
51
    /**
52
     * @param string $rulesMethod
53
     * @param array $args
54
     * @return $this
55
     */
56 6
    protected function addRules(string $rulesMethod, array $args = [])
57
    {
58 6
        $this->rules[$rulesMethod] = RuleMapping::getRule($rulesMethod, $args);
59 6
        return $this;
60
    }
61
62
    /**
63
     * @param Language $language
64
     * @param bool $break_when_error
65
     * @return bool
66
     */
67 6 View Code Duplication
    public function isValid(Language $language, $break_when_error = false) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        /** @var \PluginSimpleValidate\Contracts\Rule $rule */
70 6
        foreach ($this->rules as $ruleName => $rule) {
71 6
            if (!$rule->isValid($language, $this->value)) {
72 6
                $this->status = false;
73 6
                $this->errors[] = $rule->getError();
74
75
                /**
76
                 * break when there is any rule error
77
                 */
78 6
                if ($break_when_error === true) {
79 6
                    break;
80
                }
81
            }
82
        }
83
84 6
        if (empty($this->errors)) {
85
            $this->status = true;
86
        }
87
88 6
        return $this->status;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 2
    public function getName(): string
95
    {
96 2
        return $this->name;
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102 1
    public function getValue()
103
    {
104 1
        return $this->value;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getRules(): array
111
    {
112
        return $this->rules;
113
    }
114
115
    /**
116
     * @return array
117
     */
118 6
    public function getErrors(): array
119
    {
120 6
        return $this->errors;
121
    }
122
123
    /**
124
     * @return $this
125
     */
126 3
    public function required()
127
    {
128 3
        $this->addRules('required');
129 3
        return $this;
130
    }
131
132
    /**
133
     * @return $this
134
     */
135 2
    public function validEmail()
136
    {
137 2
        $this->addRules('valid_email');
138 2
        return $this;
139
    }
140
141
    /**
142
     * @param int|float|double|string $match
143
     * @return $this
144
     */
145 1
    public function equal($match)
146
    {
147 1
        $this->addRules('equal', [VAR_MATCH => $match]);
148 1
        return $this;
149
    }
150
151
    /**
152
     * @param int|float|double $limit
153
     * @return $this
154
     */
155 1
    public function lessThan($limit)
156
    {
157 1
        $this->addRules('less_than', [VAR_LIMIT => $limit]);
158 1
        return $this;
159
    }
160
161
    /**
162
     * @param int|float|double $lower
163
     * @param int|float|double $upper
164
     * @return $this
165
     */
166 1
    public function between($lower, $upper)
167
    {
168 1
        $this->addRules('between', [VAR_LOWER_LIMIT => $lower, VAR_UPPER_LIMIT => $upper]);
169 1
        return $this;
170
    }
171
}