diff --git a/Tests/macaddressesTest.php b/Tests/macaddressesTest.php new file mode 100644 index 0000000..1cddf08 --- /dev/null +++ b/Tests/macaddressesTest.php @@ -0,0 +1,106 @@ + + */ + +/** Test the macaddresses.php file */ +class test_macaddresses extends PHPUnit_Framework_TestCase +{ + public function test_isMACAddress_1 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress (""); + $this->assertSame (false, $res); + } + + public function test_isMACAddress_2 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress ("INVALID"); + $this->assertSame (false, $res); + } + + public function test_isMACAddress_3 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress ("de:ad:be:af:aa:bb"); + $this->assertSame (true, $res); + } + + public function test_isMACAddress_4 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress ("de-ad-be-af-aa-bb"); + $this->assertSame (true, $res); + } + + public function test_isMACAddress_5 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress ("0005.313B.9080"); + $this->assertSame (true, $res); + } + + public function test_isMACAddress_6 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress ("0005313B9080"); + $this->assertSame (true, $res); + } + + public function test_isMACAddress_7 () + { + $macaddresses = new macaddresses (); + $res = $macaddresses->isMACAddress ("a0005313B9080a"); + $this->assertSame (false, $res); + } + + public function test_addSeparator_1 () + { + $res = macaddresses::addSeparator ("0005313B9080"); + $this->assertSame ("00:05:31:3B:90:80", $res); + } + + public function test_addSeparator_2 () + { + $res = macaddresses::addSeparator ("0005313B9080", "-"); + $this->assertSame ("00-05-31-3B-90-80", $res); + } + + public function test_addSeparator_3 () + { + $res = macaddresses::addSeparator ("0005313B9080", ".", 4); + $this->assertSame ("0005.313B.9080", $res); + } + + public function test_addSeparator_4 () + { + $this->expectException (); + $res = macaddresses::addSeparator ("INVALID", ".", 4); + } + + public function test_removeSeparator_1 () + { + $res = macaddresses::removeSeparator("de:ad:be:af:aa:bb"); + $this->assertSame ("deadbeafaabb", $res); + } + + public function test_removeSeparator_2 () + { + $res = macaddresses::removeSeparator("de-ad-be-af-aa-bb"); + $this->assertSame ("deadbeafaabb", $res); + } + + public function test_removeSeparator_3 () + { + $res = macaddresses::removeSeparator("dead.beaf.aabb"); + $this->assertSame ("deadbeafaabb", $res); + } + + public function test_removeSeparator_4 () + { + $res = macaddresses::removeSeparator("deadbeafaabb"); + $this->assertSame ("deadbeafaabb", $res); + } +} diff --git a/macaddresses.php b/macaddresses.php new file mode 100644 index 0000000..fa5c80e --- /dev/null +++ b/macaddresses.php @@ -0,0 +1,70 @@ + + */ + +/** Manage the MAC addresses + * Based on : + * https://www.stetsenko.net/2011/01/php-mac-address-validating-and-formatting/ + * The MAC format can be : + * - de:ad:be:af:aa:bb + * - de-ad-be-af-aa-bb + * - 0005.313B.9080 + * - 0005313B9080 + */ +class macaddresses +{ + /** This method return true if the provided MAC address is valid + * return false if the mac is not valid + * The MAC format can be : + * - de:ad:be:af:aa:bb + * - de-ad-be-af-aa-bb + * - 0005.313B.9080 + * - 0005313B9080 + * @param string $mac The mac to check + * @return bool + */ + public static function isMACAddress ($mac) + // {{{ + { + if (! is_string ($mac)) + return false; + return ( + preg_match ("/^([a-fA-F0-9]{2}[-:]){5}[0-9A-Fa-f]{2}$/", $mac) === 1 || + preg_match ("/^([a-fA-F0-9]{4}[.]){2}[a-fA-F0-9]{4}$/", $mac) === 1 || + preg_match ("/^[a-fA-F0-9]{12}$/", $mac) === 1); + } + // }}} + + /** Reform the mac address with the separator. + * The provided mac MUST be without separator (can be removed by + * removeSeparator method) + * @param string $mac The mac address to update, without separator + * @param string|null $separator The separator to use (: by default) + * @param integer|null $nbdigit The number of digit to use + * @return string The updated mac address with separators + */ + public static function addSeparator ($mac, $separator = ':', $nbdigit = 2) + // {{{ + { + if (strspn ($mac, "0132465789ABCDEFabcdef") !== strlen ($mac)) + throw new \Exception (dgettext ("domframework", + "Invalid mac address provided to addSeparator : bad chars"), 406); + return join ($separator, str_split ($mac, $nbdigit)); + } + // }}} + + /** Remove all the separators from the provided MAC address + * @param string $mac the mac address to update + * @param array|null $separators The separators to remove + * @return the updated mac address without separators + */ + public static function removeSeparator ($mac, + $separators = array (":", "-", ".")) + // {{{ + { + return str_replace ($separators, '', $mac); + } + // }}} +}