Passed
Push — master ( 6c42be...776013 )
by Igor
02:03
created

EnumerableConstants::enum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\Core
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\Core\Base;
12
13
/**
14
 * PHP version 5.4 and 7
15
 *
16
 * @package   Payever\Core
17
 * @author    Hennadii.Shymanskyi <[email protected]>
18
 * @copyright 2017-2019 payever GmbH
19
 * @license   MIT <https://opensource.org/licenses/MIT>
20
 */
21
abstract class EnumerableConstants
22
{
23
    /**
24
     * @return array
25
     *
26
     * @throws \ReflectionException
27
     */
28
    public static function enum()
29
    {
30
        $ref = new \ReflectionClass(new static());
31
        $constants = $ref->getConstants();
32
33
        return $constants;
34
    }
35
36
    /**
37
     * @param mixed $value
38
     * @return mixed
39
     *
40
     * @throws \ReflectionException
41
     */
42
    public static function constantNameByValue($value)
43
    {
44
        foreach (self::enum() as $name => $val) {
45
            if ($val === $value) {
46
                return $name;
47
            }
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * @param string $name
55
     *
56
     * @return mixed|null
57
     *
58
     * @throws \ReflectionException
59
     */
60
    public static function valueOf($name)
61
    {
62
        $enums = self::enum();
63
64
        return array_key_exists($name, $enums) ? $enums[$name] : null;
65
    }
66
}
67