Completed
Push — master ( 3d7536...3ced6b )
by Marcin
02:56
created

Mac::clean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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