Completed
Push — master ( caad92...bf4c7a )
by Peter
05:39
created

IPv4IntervalPoint::eq()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php 
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Component\Interval\IPv4;
11
12
use GpsLab\Component\Interval\Exception\InvalidPointTypeException;
13
use GpsLab\Component\Interval\PointInterface;
14
15
class IPv4IntervalPoint implements PointInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $ip;
21
22
    /**
23
     * @var int
24
     */
25
    private $long;
26
27
    /**
28
     * @param string $ip
29
     */
30
    public function __construct($ip)
31
    {
32
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
33
            throw InvalidPointTypeException::create('IPv4', $ip);
34
        }
35
36
        $this->ip = $ip;
37
        $this->long = ip2long($ip);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function value()
44
    {
45
        return $this->long;
46
    }
47
48
    /**
49
     * @param IPv4IntervalPoint $point
50
     *
51
     * @return bool
52
     */
53
    public function eq(IPv4IntervalPoint $point)
54
    {
55
        return $this->value() == $point->value();
56
    }
57
58
    /**
59
     * @param IPv4IntervalPoint $point
60
     *
61
     * @return bool
62
     */
63
    public function neq(IPv4IntervalPoint $point)
64
    {
65
        return $this->value() != $point->value();
66
    }
67
68
    /**
69
     * @param IPv4IntervalPoint $point
70
     *
71
     * @return bool
72
     */
73
    public function lt(IPv4IntervalPoint $point)
74
    {
75
        return $this->value() < $point->value();
76
    }
77
78
    /**
79
     * @param IPv4IntervalPoint $point
80
     *
81
     * @return bool
82
     */
83
    public function lte(IPv4IntervalPoint $point)
84
    {
85
        return $this->value() <= $point->value();
86
    }
87
88
    /**
89
     * @param IPv4IntervalPoint $point
90
     *
91
     * @return bool
92
     */
93
    public function gt(IPv4IntervalPoint $point)
94
    {
95
        return $this->value() > $point->value();
96
    }
97
98
    /**
99
     * @param IPv4IntervalPoint $point
100
     *
101
     * @return bool
102
     */
103
    public function gte(IPv4IntervalPoint $point)
104
    {
105
        return $this->value() >= $point->value();
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function __toString()
112
    {
113
        return $this->ip;
114
    }
115
}
116