Passed
Pull Request — master (#56)
by Brent
01:34
created

Enum::findDefinition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Spatie\Enum;
4
5
use BadMethodCallException;
6
use ReflectionClass;
7
8
/**
9
 * @property-read string value
10
 * @property-read string label
11
 */
12
abstract class Enum
13
{
14
    /** @var mixed */
15
    protected $value;
16
17
    protected string $label;
18
19
    private static array $definitionCache = [];
20
21
    public static function toArray(): array
22
    {
23
        $array = [];
24
25
        foreach (static::resolveDefinition() as $definition) {
26
            $array[$definition->value] = $definition->label;
27
        }
28
29
        return $array;
30
    }
31
32
    /**
33
     * @param mixed $value
34
     */
35
    public function __construct($value)
36
    {
37
        $definition = $this->findDefinition($value);
38
39
        if ($definition === null) {
40
            $enumClass = static::class;
41
42
            throw new BadMethodCallException("There's no value {$value} defined for enum {$enumClass}, consider adding it in the docblock definition.");
43
        }
44
45
        $this->value = $definition->value;
0 ignored issues
show
Bug introduced by
The property value is declared read-only in Spatie\Enum\Enum.
Loading history...
46
        $this->label = $definition->label;
0 ignored issues
show
Bug introduced by
The property label is declared read-only in Spatie\Enum\Enum.
Loading history...
47
    }
48
49
    public function __get($name)
50
    {
51
        if ($name === 'label') {
52
            return $this->label;
53
        }
54
55
        if ($name === 'value') {
56
            return $this->value;
57
        }
58
    }
59
60
    public static function __callStatic(string $name, array $arguments)
61
    {
62
        return new static($name);
63
    }
64
65
    public function __call($name, $arguments)
66
    {
67
        if (strpos($name, 'is') === 0) {
68
            $other = new static(lcfirst(str_replace('is', '', $name)));
69
70
            return $this->equals($other);
71
        }
72
    }
73
74
    public function equals(Enum ...$others): bool
75
    {
76
        foreach ($others as $other) {
77
            if (
78
                get_class($this) === get_class($other)
79
                && $this->value === $other->value
80
            ) {
81
                return true;
82
            }
83
        }
84
85
        return false;
86
    }
87
88
    protected static function values(): array
89
    {
90
        return [];
91
    }
92
93
    protected static function labels(): array
94
    {
95
        return [];
96
    }
97
98
    private function findDefinition(string $input): ?EnumDefinition
99
    {
100
        foreach (static::resolveDefinition() as $definition) {
101
            if ($definition->equals($input)) {
102
                return $definition;
103
            }
104
        }
105
106
        return null;
107
    }
108
109
    /**
110
     * @return \Spatie\Enum\EnumDefinition[]|null
111
     */
112
    private static function resolveDefinition(): array
113
    {
114
        $className = static::class;
115
116
        if (static::$definitionCache[$className] ?? null) {
0 ignored issues
show
Bug introduced by
Since $definitionCache is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $definitionCache to at least protected.
Loading history...
117
            return static::$definitionCache[$className];
118
        }
119
120
        $reflectionClass = new ReflectionClass($className);
121
122
        $docComment = $reflectionClass->getDocComment();
123
124
        preg_match_all('/@method static self ([\w_]+)\(\)/', $docComment, $matches);
125
126
        $definition = [];
127
128
        $valueMap = static::values();
129
130
        $labelMap = static::labels();
131
132
        foreach ($matches[1] as $methodName) {
133
            $definition[$methodName] = new EnumDefinition(
134
                $methodName,
135
                $valueMap[$methodName] ?? $methodName,
136
                $labelMap[$methodName] ?? $methodName,
137
            );
138
        }
139
140
        return static::$definitionCache[$className] ??= $definition;
141
    }
142
}
143