ClassEnum::getClassFromName()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of PHP DNS Server.
5
 *
6
 * (c) Yif Swery <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace yswery\DNS;
13
14
class ClassEnum
15
{
16
    public const INTERNET = 1;
17
    public const CSNET = 2;
18
    public const CHAOS = 3;
19
    public const HESIOD = 4;
20
21
    /**
22
     * @var array
23
     */
24
    public static $classes = [
25
        self::INTERNET => 'IN',
26
        self::CSNET => 'CS',
27
        self::CHAOS => 'CHAOS',
28
        self::HESIOD => 'HS',
29
    ];
30
31
    /**
32
     * Determine if a class is valid.
33
     *
34
     * @param string $class
35
     *
36
     * @return bool
37
     */
38
    public static function isValid($class): bool
39
    {
40
        return array_key_exists($class, self::$classes);
41
    }
42
43
    /**
44
     * @param int $class
45
     *
46
     * @return mixed
47
     *
48
     * @throws \InvalidArgumentException
49
     */
50
    public static function getName(int $class): string
51
    {
52
        if (!static::isValid($class)) {
53
            throw new \InvalidArgumentException(sprintf('No class matching integer "%s"', $class));
54
        }
55
56
        return self::$classes[$class];
57
    }
58
59
    /**
60
     * @param string $name
61
     *
62
     * @return int
63
     */
64 43
    public static function getClassFromName(string $name): int
65
    {
66 43
        $class = array_search(strtoupper($name), self::$classes, true);
67
68 43
        if (false === $class || !is_int($class)) {
69 1
            throw new \InvalidArgumentException(sprintf('Class: "%s" is not defined.', $name));
70
        }
71
72 43
        return $class;
73
    }
74
}
75