Enum::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace JimmyOak\DataType;
4
5
abstract class Enum extends SimpleValueObject
6
{
7
    /**
8
     * @param mixed $value
9
     */
10
    public function __construct($value)
11
    {
12
        $this->guardAgainstValueNotInValidConstants($value);
13
        parent::__construct($value);
14
    }
15
16
    /**
17
     * @return array
18
     */
19
    public static function getConstList()
20
    {
21
        static $constants = [];
22
23
        if (!isset($constants[static::class])) {
24
            $constants[static::class] = (new \ReflectionClass(static::class))->getConstants();
25
        }
26
27
        return $constants[static::class];
28
    }
29
30
    private function guardAgainstValueNotInValidConstants($value)
31
    {
32
        if (!in_array($value, self::getConstList(), true)) {
33
            throw new \InvalidArgumentException(
34
                sprintf('Provided value for %s is not valid: %s', static::class, $value)
35
            );
36
        }
37
    }
38
}
39