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

Enum::arrayHasDuplicates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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