Completed
Push — master ( 00a863...d2dd08 )
by thomas
50:05 queued 12:04
created

Ipv4::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Ip;
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class Ipv4 implements IpInterface
9
{
10
    const MAGIC = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff";
11
12
    /**
13
     * @var string
14
     */
15
    private $ip;
16
17
    /**
18
     * Ipv4 constructor.
19
     * @param string $ip
20
     */
21
    public function __construct($ip)
22
    {
23
        if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
24
            throw new \InvalidArgumentException('IPv4: a valid IPv4 address is required');
25
        }
26
27
        $this->ip = $ip;
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function getHost()
34
    {
35
        return $this->ip;
36
    }
37
38
    /**
39
     * @return BufferInterface
40
     */
41
    public function getBuffer()
42
    {
43
        return new Buffer(self::MAGIC . pack("N", ip2long($this->ip)));
44
    }
45
}
46