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

Str::concreteValidate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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 if provided value is a string.
16
 *
17
 */
18
class Str extends AbstractString implements RuleSanitizeInterface, RuleValidateInterface
19
{
20
    /**
21
     * @var array Rule properties
22
     */
23
    public static $config = [
24
        'class' => 'Str1ng',
25
        'full_class' => __CLASS__,
26
        'alias' => ['string', 'str', 's'],
27
        'args_count' => 0,
28
        'args_type' => [],
29
        'has_validate' => true,
30
        //'has_sanitize' => true
31
    ];
32
33
    /**
34
     * @var string Error message
35
     */
36
    private $message = '';
37
38
    /**
39
     * Validate.
40
     *
41
     * @return bool
42
     */
43 8
    public function validate(): bool
44
    {
45 8
        $args = func_get_args();
46
47 8
        return $this->concreteValidate($args[0]);
48
    }
49
50
    /**
51
     * Concrete validate.
52
     *
53
     * @param mixed $received
54
     *
55
     * @return bool
56
     */
57 8
    private function concreteValidate($received): bool
58
    {
59 8
        if (!is_string($received)) {
60 5
            $this->message = "Received value is not a string";
61 5
            return true;
62
        }
63
64 3
        return false;
65
    }
66
67
    /**
68
     * Return error message.
69
     *
70
     * @return string Error message
71
     */
72
    public function getMessage(): string
73
    {
74
        return $this->message;
75
    }
76
}
77