1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Validator |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 pudelek.org.pl |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view source file |
10
|
|
|
* that is bundled with this package in the file LICENSE |
11
|
|
|
* |
12
|
|
|
* @author Marcin Pudełek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare (strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace mrcnpdlk\Validator\Types; |
18
|
|
|
|
19
|
|
|
use mrcnpdlk\Validator\Exception; |
20
|
|
|
use mrcnpdlk\Validator\TypeInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Mac |
24
|
|
|
* |
25
|
|
|
* @package mrcnpdlk\Validator\Types |
26
|
|
|
*/ |
27
|
|
|
class Mac extends TypeAbstract implements TypeInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param mixed $checkedValue |
31
|
|
|
* @param bool $asEx |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
* @throws \mrcnpdlk\Validator\Exception |
35
|
|
|
*/ |
36
|
5 |
View Code Duplication |
public static function isValid($checkedValue, bool $asEx = false): bool |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
try { |
39
|
5 |
|
static::isValidType($checkedValue, static::TYPE_STRING, true); |
40
|
|
|
|
41
|
5 |
|
if (!preg_match('/^[0-9a-fA-F]{2}(?=([:;.]?))(?:\\1[0-9a-fA-F]{2}){5}$/', $checkedValue)) { |
42
|
4 |
|
throw new \Exception("Regexp error", 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
return true; |
46
|
4 |
|
} catch (\Exception $e) { |
47
|
4 |
|
if ($asEx) { |
48
|
3 |
|
throw new Exception(sprintf("Invalid MAC address [%s], reason: %s", $checkedValue, $e->getMessage())); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Triming and removing separators |
57
|
|
|
* |
58
|
|
|
* @param $checkedValue |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
4 |
|
public static function clean($checkedValue) |
63
|
|
|
{ |
64
|
4 |
|
static::isValidType($checkedValue, static::TYPE_STRING, true); |
65
|
|
|
|
66
|
3 |
|
return preg_replace('/[^a-f\d]/i', '', trim(strtolower($checkedValue))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param bool $setUpper |
71
|
|
|
* |
72
|
|
|
* @return mixed|string |
73
|
|
|
*/ |
74
|
1 |
|
public function getShort(bool $setUpper = false) |
75
|
|
|
{ |
76
|
1 |
|
return $setUpper ? strtoupper($this->get()) : $this->get(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $separator |
81
|
|
|
* @param bool $setUpper |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getLong(string $separator = ':', bool $setUpper = false) |
86
|
|
|
{ |
87
|
1 |
|
return implode($separator, str_split($setUpper ? strtoupper($this->get()) : $this->get(), 2)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get device vendor |
92
|
|
|
* |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
|
|
public function getVendor() |
96
|
|
|
{ |
97
|
|
|
$url = sprintf('%s%s', "http://api.macvendors.com/", $this->get()); |
98
|
|
|
$ch = curl_init(); |
99
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
100
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
101
|
|
|
$response = curl_exec($ch); |
102
|
|
|
if ($response) { |
103
|
|
|
return $response; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.