IpVN::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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