Completed
Push — master ( 6c2145...a9825a )
by Nicolò
01:57
created

MacAddress::toStringWithDot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace ValueObjects\Identity;
4
5
use ValueObjects\Exception\InvalidNativeArgumentException;
6
use ValueObjects\Number\Natural;
7
8
final class MacAddress extends Natural
9
{
10
    public function __construct($value)
11
    {
12
        $options = array(
13
            'options' => array(
14
                'max_range' => pow(2, 48) - 1
15
            )
16
        );
17
18
        $value = filter_var($value, FILTER_VALIDATE_INT, $options);
19
20
        if (false === $value) {
21
            throw new InvalidNativeArgumentException($value, array('mac address (<= 281,474,976,710,655)'));
22
        }
23
24
        parent::__construct($value);
25
    }
26
27
    /**
28
     * @param string $value
29
     * @return MacAddress
30
     * @throws InvalidNativeArgumentException
31
     */
32
    public static function fromString($value)
33
    {
34
        $filteredValue = filter_var($value, FILTER_VALIDATE_MAC);
35
36
        if ($filteredValue === false) {
37
            throw new InvalidNativeArgumentException($value, array('string (valid Mac address)'));
38
        }
39
40
        return new self(hexdec(str_replace(['-', ':', '.'], '', $filteredValue)));
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function toStringWithDash()
47
    {
48
        return trim(chunk_split(str_pad(dechex($this->value), 12, '0', STR_PAD_LEFT), 2, '-'), '-');
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function toStringWithColon()
55
    {
56
        return trim(chunk_split(str_pad(dechex($this->value), 12, '0', STR_PAD_LEFT), 2, ':'), ':');
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function toStringWithDot()
63
    {
64
        return trim(chunk_split(str_pad(dechex($this->value), 12, '0', STR_PAD_LEFT), 4, '.'), '.');
65
    }
66
}
67