AbstractEnum   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 60
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnum() 0 11 2
A isValidName() 0 12 2
A isValidValue() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Runalyze Age Grade.
5
 *
6
 * (c) RUNALYZE <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Runalyze\Common\Enum;
13
14
/**
15
 * Abstract class for enums.
16
 *
17
 * Usage:
18
 * <code>
19
 * class DaysOfWeek extends AbstractEnum
20
 * {
21
 *   const SUNDAY = 0;
22
 *   const MONDAY = 1;
23
 *   // ...
24
 * }
25
 *
26
 * DaysOfWeek::isValidName('Monday'); // true
27
 * DaysOfWeek::isValidName('Monday', true); // false
28
 * DaysOfWeek::isValidValue(1); // true
29
 * DaysOfWeek::isValidValue(123); // false
30
 * DaysOfWeek::getEnum(); // array('SUNDAY' => 0, ...)
31
 * </code>
32
 *
33
 * @see http://stackoverflow.com/a/254543/3449264
34
 */
35
abstract class AbstractEnum
36
{
37
    /** @var array */
38
    private static $ConstantsCache = [];
39
40
    /**
41
     * Get all constants of called class.
42
     *
43
     * This static methods uses an internal cache to not create a reflection on every call.
44
     *
45
     * @return array a hash with your constants and their value
46
     */
47 4
    public static function getEnum()
48
    {
49 4
        $enumClass = get_called_class();
50
51 4
        if (!isset(self::$ConstantsCache[$enumClass])) {
52 2
            $reflect = new \ReflectionClass($enumClass);
53 2
            self::$ConstantsCache[$enumClass] = $reflect->getConstants();
54 2
        }
55
56 4
        return self::$ConstantsCache[$enumClass];
57
    }
58
59
    /**
60
     * Checks whether a constant is valid.
61
     *
62
     * @param string $name   name of the constant
63
     * @param bool   $strict whether to make a case sensitive check
64
     *
65
     * @return bool the result of the test
66
     */
67 1
    public static function isValidName($name, $strict = false)
68
    {
69 1
        $constants = self::getEnum();
70
71 1
        if ($strict) {
72 1
            return array_key_exists($name, $constants);
73
        }
74
75 1
        $keys = array_map('strtolower', array_keys($constants));
76
77 1
        return in_array(strtolower($name), $keys, true);
78
    }
79
80
    /**
81
     * Checks whether a value is defined.
82
     *
83
     * @param int|string $value  the value to test
84
     * @param bool       $strict check the types of the value in the values
85
     *
86
     * @return bool the result of the test
87
     */
88 1
    public static function isValidValue($value, $strict = true)
89
    {
90 1
        $values = array_values(self::getEnum());
91
92 1
        return in_array($value, $values, $strict);
93
    }
94
}
95