Completed
Push — master ( c35835...32add1 )
by Zbigniew
02:35
created

AbstractEnum::isValidKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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