Classes::getClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS;
15
16
class Classes
17
{
18
    const INTERNET = 'IN';
19
    const CSNET = 'CS';
20
    const CHAOS = 'CH';
21
    const HESIOD = 'HS';
22
23
    /**
24
     * @var array
25
     */
26
    public static $classes = [
27
        self::CHAOS => 'CHAOS',
28
        self::CSNET => 'CSNET',
29
        self::HESIOD => 'Hesiod',
30
        self::INTERNET => 'Internet',
31
    ];
32
33
    const CLASS_IDS = [
34
        self::CHAOS => 3,
35
        self::CSNET => 2,
36
        self::HESIOD => 4,
37
        self::INTERNET => 1,
38
    ];
39
40
    /**
41
     * @const string[]
42
     */
43
    const IDS_CLASSES = [
44
        1 => 'IN',
45
        2 => 'CS',
46
        3 => 'CH',
47
        4 => 'HS',
48
    ];
49
50
    /**
51
     * Determine if a class is valid.
52
     */
53 53
    public static function isValid(string $class): bool
54
    {
55 53
        if (array_key_exists($class, self::$classes)) {
56 49
            return true;
57
        }
58
59 33
        return 1 === preg_match('/^CLASS\d+$/', $class);
60
    }
61
62
    /**
63
     * @throws \InvalidArgumentException
64
     */
65 49
    public static function getClassId(string $className): int
66
    {
67 49
        if (!self::isValid($className)) {
68 1
            throw new \InvalidArgumentException(sprintf('Class "%s" is not a valid DNS class.', $className));
69
        }
70
71 48
        if (1 === preg_match('/^CLASS(\d+)$/', $className, $matches)) {
72 2
            return (int) $matches[1];
73
        }
74
75 47
        return self::CLASS_IDS[$className];
76
    }
77
78 40
    public static function getClassName(int $classId): string
79
    {
80 40
        if (array_key_exists($classId, self::IDS_CLASSES)) {
81 39
            return self::IDS_CLASSES[$classId];
82
        }
83
84 2
        return 'CLASS'.$classId;
85
    }
86
}
87