Completed
Push — master ( e9d63e...e9806f )
by Zbigniew
11s queued 10s
created

AbstractEnum::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-library package.
5
 *
6
 * (c) Zbigniew Ślązak
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 Zibios\WrikePhpLibrary\Enum;
13
14
/**
15
 * Abstract Enum.
16
 *
17
 * @SuppressWarnings(PHPMD.TooManyFields)
18
 */
19
abstract class AbstractEnum
20
{
21
    /**
22
     * @var array
23
     */
24
    protected static $cache = [];
25
26
    /**
27
     * Return all constants as Key => Value array.
28
     *
29
     * @return array
30
     */
31 292
    public static function toArray()
32
    {
33 292
        $class = get_called_class();
34 292
        if (!array_key_exists($class, static::$cache)) {
35
            $reflection = new \ReflectionClass($class);
36
            static::$cache[$class] = $reflection->getConstants();
37
        }
38
39 292
        return static::$cache[$class];
40
    }
41
42
    /**
43
     * Return all constants Keys as array.
44
     *
45
     * @return array|string[]
46
     */
47 32
    public static function getKeys()
48
    {
49 32
        return array_keys(static::toArray());
50
    }
51
52
    /**
53
     * Return Enum Key for requested Value.
54
     *
55
     * @param mixed $value
56
     *
57
     * @throws \InvalidArgumentException
58
     *
59
     * @return string
60
     */
61 16
    public static function getKey($value)
62
    {
63 16
        self::assertIsValidValue($value);
64
65 16
        return (string) array_search($value, static::toArray(), true);
66
    }
67
68
    /**
69
     * Validate if Key is valid.
70
     *
71
     * @param string $key
72
     *
73
     * @return bool
74
     */
75 80
    public static function isValidKey($key)
76
    {
77 80
        return array_key_exists($key, self::toArray());
78
    }
79
80
    /**
81
     * Throw exception if Key is not valid.
82
     *
83
     * @param string $key
84
     *
85
     * @throws \InvalidArgumentException
86
     */
87 80
    public static function assertIsValidKey($key)
88
    {
89 80
        if (false === self::isValidKey($key)) {
90 48
            throw new \InvalidArgumentException('Wrong key.');
91
        }
92 32
    }
93
94
    /**
95
     * Return Enum Value for requested Key.
96
     *
97
     * @param string $key
98
     *
99
     * @throws \InvalidArgumentException
100
     *
101
     * @return mixed
102
     */
103 16
    public static function getValue($key)
104
    {
105 16
        self::assertIsValidKey($key);
106
107 16
        return static::toArray()[$key];
108
    }
109
110
    /**
111
     * Return all constants Values as array.
112
     *
113
     * @return array
114
     */
115 32
    public static function getValues()
116
    {
117 32
        return array_values(self::toArray());
118
    }
119
120
    /**
121
     * Validate if Value is valid.
122
     *
123
     * @param mixed $value
124
     *
125
     * @return bool
126
     */
127 164
    public static function isValidValue($value)
128
    {
129 164
        return in_array($value, static::toArray(), true);
130
    }
131
132
    /**
133
     * Throw exception if Value is not valid.
134
     *
135
     * @param mixed $value
136
     *
137
     * @throws \InvalidArgumentException
138
     */
139 164
    public static function assertIsValidValue($value)
140
    {
141 164
        if (false === self::isValidValue($value)) {
142 48
            throw new \InvalidArgumentException('Wrong value.');
143
        }
144 116
    }
145
}
146