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

Ip   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 60
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 5 1
A concreteValidate() 0 12 3
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 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