Completed
Push — master ( 282f78...6dce89 )
by Sam
05:20
created

ClassEnum   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassFromName() 0 3 1
A isValid() 0 3 1
A getName() 0 7 2
1
<?php
2
/*
3
 * This file is part of PHP DNS Server.
4
 *
5
 * (c) Yif Swery <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace yswery\DNS;
12
13
class ClassEnum
14
{
15
    public const INTERNET = 1;
16
    public const CSNET = 2;
17
    public const CHAOS = 3;
18
    public const HESIOD = 4;
19
20
    /**
21
     * @var array
22
     */
23
    public static $classes = [
24
        self::INTERNET => 'IN',
25
        self::CSNET => 'CS',
26
        self::CHAOS => 'CHAOS',
27
        self::HESIOD => 'HS',
28
    ];
29
30
    /**
31
     * Determine if a class is valid.
32
     *
33
     * @param string $class
34
     *
35
     * @return bool
36
     */
37
    public static function isValid($class): bool
38
    {
39
        return array_key_exists($class, self::$classes);
40
    }
41
42
    /**
43
     * @param int $class
44
     *
45
     * @return mixed
46
     *
47
     * @throws \InvalidArgumentException
48
     */
49
    public static function getName(int $class): string
50
    {
51
        if (!static::isValid($class)) {
52
            throw new \InvalidArgumentException(sprintf('No class matching integer "%s"', $class));
53
        }
54
55
        return self::$classes[$class];
56
    }
57
58
    /**
59
     * @param string $name
60
     *
61
     * @return int
62
     */
63
    public static function getClassFromName(string $name): int
64
    {
65
        return array_search($name, self::$classes, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($name, self::classes, true) could return the type false|string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
}
68