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