1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Character\Properties; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Comparable; |
6
|
|
|
use UCD\Exception\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
abstract class Enumeration implements Comparable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var mixed |
12
|
|
|
*/ |
13
|
|
|
private $value; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private static $constantsCache = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param mixed $value |
22
|
|
|
* @throws InvalidArgumentException |
23
|
|
|
*/ |
24
|
|
|
final public function __construct($value) |
25
|
|
|
{ |
26
|
|
|
if ($this->isKnown($value) !== true) { |
27
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not recognised', var_export($value, true))); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->value = $value; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $value |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
|
|
final public static function fromValue($value) |
38
|
|
|
{ |
39
|
|
|
return new static($value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
final public function getValue() |
46
|
|
|
{ |
47
|
|
|
return $this->value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $other |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function equals($other) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if ($this === $other) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $other instanceof static |
61
|
|
|
&& $other->value === $this->value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $value |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
private function isKnown($value) |
69
|
|
|
{ |
70
|
|
|
return in_array($value, self::getConstants(), true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
private static function getConstants() |
77
|
|
|
{ |
78
|
|
|
$className = static::class; |
79
|
|
|
|
80
|
|
|
if (!array_key_exists($className, self::$constantsCache)) { |
81
|
|
|
self::$constantsCache[$className] = self::getConstantsForClass($className); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return self::$constantsCache[$className]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $className |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
private static function getConstantsForClass($className) |
92
|
|
|
{ |
93
|
|
|
return (new \ReflectionClass($className)) |
94
|
|
|
->getConstants(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function __toString() |
101
|
|
|
{ |
102
|
|
|
return (string)$this->value; |
103
|
|
|
} |
104
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.