MacAddress::toStringWithDash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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