Ip   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 58
ccs 12
cts 12
cp 1
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
        'full_class' => __CLASS__,
25
        'alias' => ['ip'],
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 221
    public function validate(): bool
41
    {
42 221
        $args = \func_get_args();
43
44 221
        return $this->concreteValidate($args[0]);
45
    }
46
47
    /**
48
     * Concrete validate.
49
     *
50
     * @param string $received
51
     *
52
     * @return bool
53
     */
54 221
    private function concreteValidate(string $received): bool
55
    {
56 221
        if (\filter_var($received, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
57 56
            return false;
58
        }
59
60 165
        if (\filter_var($received, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
61 158
            return false;
62
        }
63
64 7
        $this->message = 'Received value is not a valid ip address';
65 7
        return true;
66
    }
67
68
    /**
69
     * Return error message.
70
     *
71
     * @return string Error message
72
     */
73 1
    public function getMessage(): string
74
    {
75 1
        return $this->message;
76
    }
77
}
78