EnumerableConstants::constantNameByValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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