Passed
Push — master ( 590085...649359 )
by
unknown
37s
created

Field::addRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 2/26/18
6
 * Time: 4:53 PM
7
 */
8
9
namespace PluginSimpleValidate\MultiValues;
10
11
use PluginSimpleValidate\Contracts\MultiValuesField;
12
use PluginSimpleValidate\BaseAbstract\RuleMapping as AbstractRuleMapping;
13
use PluginSimpleValidate\Contracts\RuleWithValue;
14
use PluginSimpleValidate\Libraries\Language;
15
16
class Field extends \PluginSimpleValidate\BaseAbstract\Field implements MultiValuesField
17
{
18
    /**
19
     * @param Language $language
20
     * @return bool
21
     */
22 3 View Code Duplication
    public function isValid(Language $language) : 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...
23
    {
24
        // empty the `errors` array
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25 3
        $this->emptyErrors();
26
27
        /** @var RuleWithValue $rule */
28 3
        foreach ($this->rules as $ruleName => $rule) {
29 3
            if (!$rule->isValid($language, $rule->getValue())) {
30 3
                $this->status = false;
31 3
                $this->errors[] = $rule->getError();
32
            }
33
        }
34
35 3
        if (empty($this->errors)) {
36
            $this->status = true;
37
        }
38
39 3
        return $this->status;
40
    }
41
42
    /**
43
     * @param string $rulesMethod
44
     * @param mixed $value
45
     * @param array $args
46
     * @return $this
47
     */
48 3
    public function addRules(string $rulesMethod, $value, array $args = [])
49
    {
50 3
        $this->rules[uniqid(rand(), true) . $rulesMethod] = \PluginSimpleValidate\Libraries\MultiValues\RuleMapping::getInstance()->getRule($rulesMethod, $value, $args);
51 3
        return $this;
52
    }
53
54
    /**
55
     * Field constructor.
56
     * @param string $name
57
     */
58 4
    public function __construct(string $name)
59
    {
60 4
        parent::__construct($name, null);
61 4
    }
62
63
    /**
64
     * @param $value
65
     * @param string $message
66
     * @return $this
67
     */
68 2
    public function isTrue($value, string $message = '')
69
    {
70 2
        $this->addRules(AbstractRuleMapping::VALIDATE_IS_TRUE, $value, [static::VAR_MESSAGE => $message]);
71 2
        return $this;
72
    }
73
74
    /**
75
     * @param mixed $value
76
     * @return $this
77
     */
78 1
    public function required($value)
79
    {
80 1
        $this->addRules(AbstractRuleMapping::VALIDATE_REQUIRED, $value);
81 1
        return $this;
82
    }
83
84
    /**
85
     * @param mixed $value
86
     * @return $this
87
     */
88
    public function notEmpty($value)
89
    {
90
        $this->addRules(AbstractRuleMapping::VALIDATE_IS_NOT_EMPTY, $value);
91
        return $this;
92
    }
93
94
    /**
95
     * @param mixed $value
96
     * @return $this
97
     */
98 1
    public function validEmail($value)
99
    {
100 1
        $this->addRules(AbstractRuleMapping::VALIDATE_EMAIL, $value);
101 1
        return $this;
102
    }
103
104
    /**
105
     * @param mixed $value
106
     * @param float|int|string $match
107
     * @return $this
108
     */
109
    public function equal($value, $match)
110
    {
111
        $this->addRules(AbstractRuleMapping::VALIDATE_EQUAL, $value, [static::VAR_MATCH => $match]);
112
        return $this;
113
    }
114
115
    /**
116
     * @param mixed $value
117
     * @return $this
118
     */
119
    public function isNumber($value)
120
    {
121
        $this->addRules(AbstractRuleMapping::VALIDATE_NUMBER, $value);
122
        return $this;
123
    }
124
125
    /**
126
     * @param mixed $value
127
     * @return $this
128
     */
129
    public function isAlpha($value)
130
    {
131
        $this->addRules(AbstractRuleMapping::VALIDATE_ALPHA, $value);
132
        return $this;
133
    }
134
135
    /**
136
     * @param mixed $value
137
     * @return $this
138
     */
139
    public function isAlphaOrNumeric($value)
140
    {
141
        $this->addRules(AbstractRuleMapping::VALIDATE_ALPHA_OR_NUMERIC, $value);
142
        return $this;
143
    }
144
145
    /**
146
     * @param mixed $value
147
     * @return $this
148
     */
149
    public function isDecimal($value)
150
    {
151
        $this->addRules(AbstractRuleMapping::VALIDATE_DECIMAL, $value);
152
        return $this;
153
    }
154
155
    /**
156
     * @param mixed $value
157
     * @return $this
158
     */
159
    public function isInteger($value)
160
    {
161
        $this->addRules(AbstractRuleMapping::VALIDATE_INTEGER, $value);
162
        return $this;
163
    }
164
165
    /**
166
     * @param mixed $value
167
     * @return $this
168
     */
169
    public function isNatural($value)
170
    {
171
        $this->addRules(AbstractRuleMapping::VALIDATE_NATURAL_NUMBER, $value);
172
        return $this;
173
    }
174
175
    /**
176
     * @param mixed $value
177
     * @return $this
178
     */
179
    public function isNaturalNoZero($value)
180
    {
181
        $this->addRules(AbstractRuleMapping::VALIDATE_NATURAL_NO_ZERO_NUMBER, $value);
182
        return $this;
183
    }
184
185
    /**
186
     * @param mixed $value
187
     * @param float|int $limit
188
     * @return $this
189
     */
190
    public function lessThan($value, $limit)
191
    {
192
        $this->addRules(AbstractRuleMapping::VALIDATE_LESS_THAN, $value, [static::VAR_LIMIT => $limit]);
193
        return $this;
194
    }
195
196
    /**
197
     * @param mixed $value
198
     * @param float|int $limit
199
     * @return $this
200
     */
201
    public function greaterThan($value, $limit)
202
    {
203
        $this->addRules(AbstractRuleMapping::VALIDATE_GREATER_THAN, $value, [static::VAR_LIMIT => $limit]);
204
        return $this;
205
    }
206
207
    /**
208
     * @param mixed $value
209
     * @param float|int $limit
210
     * @return $this
211
     */
212
    public function lessOrEqualThan($value, $limit)
213
    {
214
        $this->addRules(AbstractRuleMapping::VALIDATE_LESS_OR_EQUAL_THAN, $value, [static::VAR_LIMIT => $limit]);
215
        return $this;
216
    }
217
218
    /**
219
     * @param mixed $value
220
     * @param float|int $limit
221
     * @return $this
222
     */
223
    public function greaterOrEqualThan($value, $limit)
224
    {
225
        $this->addRules(AbstractRuleMapping::VALIDATE_GREATER_OR_EQUAL_THAN, $value, [static::VAR_LIMIT => $limit]);
226
        return $this;
227
    }
228
229
    /**
230
     * @param mixed $value
231
     * @param float|int $lower
232
     * @param float|int $upper
233
     * @return $this
234
     */
235
    public function between($value, $lower, $upper)
236
    {
237
        $this->addRules(AbstractRuleMapping::VALIDATE_BETWEEN, $value, [static::VAR_LOWER_LIMIT => $lower, static::VAR_UPPER_LIMIT => $upper]);
238
        return $this;
239
    }
240
241
    /**
242
     * @param mixed $value
243
     * @param float|int $lower
244
     * @param float|int $upper
245
     * @return $this
246
     */
247
    public function betweenOrEqual($value, $lower, $upper)
248
    {
249
        $this->addRules(AbstractRuleMapping::VALIDATE_BETWEEN_OR_EQUAL_THAN, $value, [static::VAR_LOWER_LIMIT => $lower, static::VAR_UPPER_LIMIT => $upper]);
250
        return $this;
251
    }
252
253
    /**
254
     * @param mixed $value
255
     * @param float|int $limit
256
     * @return $this
257
     */
258
    public function length($value, $limit)
259
    {
260
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH, $value);
261
        return $this;
262
    }
263
264
    /**
265
     * @param mixed $value
266
     * @param float|int $limit
267
     * @return $this
268
     */
269
    public function lengthLessThan($value, $limit)
270
    {
271
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH_LESS_THAN, $value, [static::VAR_LIMIT => $limit]);
272
        return $this;
273
    }
274
275
    /**
276
     * @param mixed $value
277
     * @param float|int $limit
278
     * @return $this
279
     */
280
    public function lengthGreaterThan($value, $limit)
281
    {
282
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH_GREATER_THAN, $value, [static::VAR_LIMIT => $limit]);
283
        return $this;
284
    }
285
286
    /**
287
     * @param mixed $value
288
     * @param float|int $limit
289
     * @return $this
290
     */
291
    public function lengthLessOrEqualThan($value, $limit)
292
    {
293
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH_LESS_OR_EQUAL_THAN, $value, [static::VAR_LIMIT => $limit]);
294
        return $this;
295
    }
296
297
    /**
298
     * @param mixed $value
299
     * @param float|int $limit
300
     * @return $this
301
     */
302
    public function lengthGreaterOrEqualThan($value, $limit)
303
    {
304
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH_GREATER_OR_EQUAL_THAN, $value, [static::VAR_LIMIT => $limit]);
305
        return $this;
306
    }
307
308
    /**
309
     * @param mixed $value
310
     * @param float|int $lower
311
     * @param float|int $upper
312
     * @return $this
313
     */
314
    public function lengthBetween($value, $lower, $upper)
315
    {
316
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH_BETWEEN, $value, [static::VAR_LOWER_LIMIT => $lower, static::VAR_UPPER_LIMIT => $upper]);
317
        return $this;
318
    }
319
320
    /**
321
     * @param mixed $value
322
     * @param float|int $lower
323
     * @param float|int $upper
324
     * @return $this
325
     */
326 1
    public function lengthBetweenOrEqual($value, $lower, $upper)
327
    {
328 1
        $this->addRules(AbstractRuleMapping::VALIDATE_LENGTH_BETWEEN_OR_EQUAL_THAN, $value, [static::VAR_LOWER_LIMIT => $lower, static::VAR_UPPER_LIMIT => $upper]);
329 1
        return $this;
330
    }
331
332
    /**
333
     * @param mixed $value
334
     * @param string $region
335
     * @return $this
336
     */
337
    public function isValidPhone($value, string $region)
338
    {
339
        $this->addRules(AbstractRuleMapping::VALIDATE_PHONE_NUMBER, $value, [static::VAR_REGION => $region]);
340
        return $this;
341
    }
342
}