IpVN   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A passes() 0 4 1
A message() 0 15 3
1
<?php
2
/**
3
 * @link https://github.com/phpviet/laravel-validation
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace PHPViet\Laravel\Validation\Rules;
10
11
use PHPViet\Validation\Rules\IpVN as BaseIpVN;
12
use PHPViet\Validation\Validator;
13
14
/**
15
 * @author Vuong Minh <[email protected]>
16
 * @since 1.0.0
17
 */
18
class IpVN extends CallableRule
19
{
20
    const IPV4 = BaseIpVN::IPV4;
21
22
    const IPV6 = BaseIpVN::IPV6;
23
24
    /**
25
     * @var int|null
26
     */
27
    protected $version;
28
29
    /**
30
     * IpVN constructor.
31
     *
32
     * @param int|null $version
33
     */
34
    public function __construct(?int $version = null)
35
    {
36
        $this->version = $version;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function passes($attribute, $value): bool
43
    {
44
        return Validator::ipVN($this->version)->validate($value);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function message(): string
51
    {
52
        switch ($this->version) {
53
            case self::IPV4:
54
55
                return __('phpVietValidation::validation.ip.v4');
56
            case self::IPV6:
57
58
                return __('phpVietValidation::validation.ip.v6');
59
            default:
60
61
                return __('phpVietValidation::validation.ip.default');
62
63
        }
64
    }
65
}
66