Completed
Push — master ( 5ad706...418f81 )
by Sebastian
03:53
created

Required   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 62
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A concreteValidate() 0 13 3
A validate() 0 5 1
A getMessage() 0 3 1
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
        'class' => 'Required',
25
        'full_class' => __CLASS__,
26
        'alias' => ['required', 'req', 'rq'],
27
        'args_count' => 0,
28
        'args_type' => [],
29
        'has_validate' => true,
30
        //'has_sanitize' => false
31
    ];
32
33
    /**
34
     * @var string Error message
35
     */
36
    private $message = '';
37
38
    /**
39
     * Validate.
40
     *
41
     * @return bool
42
     */
43 17
    public function validate(): bool
44
    {
45 17
        $args = func_get_args();
46
47 17
        return $this->concreteValidate($args[0]);
48
    }
49
50
    /**
51
     * Concrete validate.
52
     *
53
     * @param mixed $received
54
     *
55
     * @return bool
56
     */
57 17
    private function concreteValidate($received): bool
58
    {
59 17
        if ($received === null) {
60 1
            $this->message = "Received value is null";
61 1
            return true;
62
        }
63
64 16
        if (!strlen((string)$received)) {
65 4
            $this->message = "Received value is a void string";
66 4
            return true;
67
        }
68
69 12
        return false;
70
    }
71
72
    /**
73
     * Return error message.
74
     *
75
     * @return string Error message
76
     */
77 3
    public function getMessage(): string
78
    {
79 3
        return $this->message;
80
    }
81
}
82