Skip to content

Commit de81e3a

Browse files
committed
Check if Enum is initialized
1 parent f32c2a4 commit de81e3a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Enum.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private function __construct($name, $value)
4949
*/
5050
public function getValue()
5151
{
52+
$this->validate();
5253
return $this->value;
5354
}
5455

@@ -59,6 +60,7 @@ public function getValue()
5960
*/
6061
public function getKey()
6162
{
63+
$this->validate();
6264
return $this->name;
6365
}
6466

@@ -67,6 +69,7 @@ public function getKey()
6769
*/
6870
public function __toString()
6971
{
72+
$this->validate();
7073
return (string)$this->value;
7174
}
7275

@@ -79,9 +82,22 @@ public function __toString()
7982
*/
8083
final public function equals(Enum $enum)
8184
{
85+
$this->validate();
8286
return $this->getValue() === $enum->getValue();
8387
}
8488

89+
/**
90+
* Check if Enum is initialized
91+
*
92+
* @throws BadMethodCallException
93+
*/
94+
private function validate()
95+
{
96+
if ($this->name === null) {
97+
throw new BadMethodCallException("Enum is not initialized");
98+
}
99+
}
100+
85101
/**
86102
* Returns the names (keys) of all constants in the Enum class
87103
*

0 commit comments

Comments
 (0)