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

Ipv4::getBuffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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