Passed
Push — master ( c25ef8...37908b )
by Zbigniew
02:39
created

AbstractEnum::assertIsValidKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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