Passed
Push — master ( 5f83c0...804b68 )
by Sebastian
04:43
created

Ip::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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