Required::concreteValidate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Filter\Rules;
13
14
/**
15
 * Check required, value passed must be not null or not 0 length string.
16
 *
17
 */
18
class Required implements RuleValidateInterface
19
{
20
    /**
21
     * @var array Rule properties
22
     */
23
    public static $config = [
24
        'full_class' => __CLASS__,
25
        'alias' => ['required', 'req', 'rq'],
26
        'args_count' => 0,
27
        'args_type' => []
28
    ];
29
30
    /**
31
     * @var string Error message
32
     */
33
    private $message = '';
34
35
    /**
36
     * Validate.
37
     *
38
     * @return bool
39
     */
40 18
    public function validate(): bool
41
    {
42 18
        $args = \func_get_args();
43
44 18
        return $this->concreteValidate($args[0]);
45
    }
46
47
    /**
48
     * Concrete validate.
49
     *
50
     * @param mixed $received
51
     *
52
     * @return bool
53
     */
54 18
    private function concreteValidate($received): bool
55
    {
56 18
        if ($received === null) {
57 2
            $this->message = "Received value is null";
58 2
            return true;
59
        }
60
61 16
        if (\strlen((string) $received) === 0) {
62 5
            $this->message = "Received value is a void string";
63 5
            return true;
64
        }
65
66 11
        return false;
67
    }
68
69
    /**
70
     * Return error message.
71
     *
72
     * @return string Error message
73
     */
74 5
    public function getMessage(): string
75
    {
76 5
        return $this->message;
77
    }
78
}
79