|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Werkspot\Enum; |
|
6
|
|
|
|
|
7
|
|
|
use BadMethodCallException; |
|
8
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use Werkspot\Enum\Util\ClassNameConverter; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractEnum |
|
14
|
|
|
{ |
|
15
|
|
|
protected $value; |
|
16
|
|
|
|
|
17
|
15 |
|
protected function __construct($value) |
|
18
|
|
|
{ |
|
19
|
15 |
|
if (!$this->isValid($value)) { |
|
20
|
4 |
|
$message = 'Value [%s] is not matching any valid value of class "%s". Valid values are [%s].'; |
|
21
|
|
|
|
|
22
|
4 |
|
throw new InvalidArgumentException(sprintf( |
|
23
|
4 |
|
$message, |
|
24
|
4 |
|
$value, |
|
25
|
4 |
|
$this->getClassName(), |
|
26
|
4 |
|
self::getValidOptionsAsString() |
|
27
|
|
|
)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
11 |
|
$this->value = $value; |
|
31
|
11 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return static |
|
35
|
|
|
*/ |
|
36
|
13 |
|
public static function get($value): self |
|
37
|
|
|
{ |
|
38
|
13 |
|
return new static($value); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
View Code Duplication |
public static function __callStatic(string $methodName, array $arguments): self |
|
42
|
|
|
{ |
|
43
|
3 |
|
foreach (self::getConstants() as $option => $value) { |
|
44
|
3 |
|
$expectedMethodName = lcfirst(Inflector::classify(strtolower($option))); |
|
45
|
3 |
|
if ($expectedMethodName === $methodName) { |
|
46
|
3 |
|
return new static($value); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
throw new BadMethodCallException(sprintf('%s::%s() does not exist', static::class, $methodName)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
View Code Duplication |
public function __call(string $methodName, array $arguments) |
|
54
|
|
|
{ |
|
55
|
3 |
|
foreach (self::getConstants() as $option => $value) { |
|
56
|
3 |
|
$isaMethodName = 'is' . ucfirst(Inflector::classify(strtolower($option))); |
|
57
|
3 |
|
if ($isaMethodName === $methodName) { |
|
58
|
3 |
|
return $this->equals(static::get($value)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
1 |
|
throw new BadMethodCallException(sprintf('%s::%s() does not exist', static::class, $methodName)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
8 |
|
public function getValue() |
|
69
|
|
|
{ |
|
70
|
8 |
|
return $this->value; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
public function equals(self $other): bool |
|
74
|
|
|
{ |
|
75
|
6 |
|
return get_class($other) === get_class($this) && $other->getValue() === $this->value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
public function __toString(): string |
|
79
|
|
|
{ |
|
80
|
3 |
|
return (string) $this->value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
15 |
|
protected function isValid($value): bool |
|
84
|
|
|
{ |
|
85
|
15 |
|
return in_array($value, self::getValidOptions(), true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
16 |
|
public static function getValidOptions(): array |
|
89
|
|
|
{ |
|
90
|
16 |
|
return array_values(self::getConstants()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
protected function getClassName(): string |
|
94
|
|
|
{ |
|
95
|
4 |
|
return ClassNameConverter::stripNameSpace(static::class); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
17 |
|
private static function getConstants(): array |
|
99
|
|
|
{ |
|
100
|
17 |
|
return (new ReflectionClass(static::class))->getConstants(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
private static function getValidOptionsAsString(): string |
|
104
|
|
|
{ |
|
105
|
4 |
|
return implode( |
|
106
|
4 |
|
', ', |
|
107
|
4 |
|
array_map(function ($option) { |
|
108
|
4 |
|
return var_export($option, true); |
|
109
|
4 |
|
}, self::getValidOptions()) |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|