Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
AbstractEnum | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
getConstants | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
values | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isValid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
5 | * word processing documents. |
6 | * |
7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
8 | * General Public License version 3 as published by the Free Software Foundation. |
9 | * |
10 | * For the full copyright and license information, please read the LICENSE |
11 | * file that was distributed with this source code. For the full list of |
12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
13 | * |
14 | * @see https://github.com/PHPOffice/PHPWord |
15 | * |
16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
17 | */ |
18 | |
19 | namespace PhpOffice\PhpWord\Shared; |
20 | |
21 | use InvalidArgumentException; |
22 | use ReflectionClass; |
23 | |
24 | abstract class AbstractEnum |
25 | { |
26 | private static $constCacheArray; |
27 | |
28 | private static function getConstants() |
29 | { |
30 | if (self::$constCacheArray == null) { |
31 | self::$constCacheArray = []; |
32 | } |
33 | $calledClass = static::class; |
34 | if (!array_key_exists($calledClass, self::$constCacheArray)) { |
35 | $reflect = new ReflectionClass($calledClass); |
36 | self::$constCacheArray[$calledClass] = $reflect->getConstants(); |
37 | } |
38 | |
39 | return self::$constCacheArray[$calledClass]; |
40 | } |
41 | |
42 | /** |
43 | * Returns all values for this enum. |
44 | * |
45 | * @return array |
46 | */ |
47 | public static function values() |
48 | { |
49 | return array_values(self::getConstants()); |
50 | } |
51 | |
52 | /** |
53 | * Returns true the value is valid for this enum. |
54 | * |
55 | * @param string $value |
56 | * |
57 | * @return bool true if value is valid |
58 | */ |
59 | public static function isValid($value) |
60 | { |
61 | $values = array_values(self::getConstants()); |
62 | |
63 | return in_array($value, $values, true); |
64 | } |
65 | |
66 | /** |
67 | * Validates that the value passed is a valid value. |
68 | * |
69 | * @param string $value |
70 | */ |
71 | public static function validate($value): void |
72 | { |
73 | if (!self::isValid($value)) { |
74 | $calledClass = static::class; |
75 | $values = array_values(self::getConstants()); |
76 | |
77 | throw new InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values)); |
78 | } |
79 | } |
80 | } |