From 5e044e6635653ab2888551501426ba44385f18d1 Mon Sep 17 00:00:00 2001 From: KartaviK Date: Thu, 25 Oct 2018 15:54:22 +0300 Subject: [PATCH 1/4] Set opportunity to create enum statically with null value --- src/Enum.php | 2 +- tests/EnumTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Enum.php b/src/Enum.php index 7a105d1..0b4c276 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -177,7 +177,7 @@ public static function search($value) public static function __callStatic($name, $arguments) { $array = static::toArray(); - if (isset($array[$name])) { + if (array_key_exists($name, $array)) { return new static($array[$name]); } diff --git a/tests/EnumTest.php b/tests/EnumTest.php index ffb6583..96d09c7 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -262,4 +262,11 @@ public function testJsonSerialize() $this->assertJsonStringEqualsJsonString('""', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING))); $this->assertJsonStringEqualsJsonString('false', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))); } + + public function testNullableEnum() + { + $this->assertNull(EnumFixture::PROBLEMATIC_NULL()->getValue()); + $this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->getValue()); + $this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->jsonSerialize()); + } } From 0b720b9019ec8f0a85d255e2399103dd93ec07e9 Mon Sep 17 00:00:00 2001 From: Roman Varkuta Date: Thu, 25 Oct 2018 17:01:40 +0300 Subject: [PATCH 2/4] Use array_key_exists() as an extra check --- src/Enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enum.php b/src/Enum.php index 0b4c276..957d3a8 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -177,7 +177,7 @@ public static function search($value) public static function __callStatic($name, $arguments) { $array = static::toArray(); - if (array_key_exists($name, $array)) { + if (isset($array[$name]) || array_key_exists($name, $array)) { return new static($array[$name]); } From 7c858b2986fc9590f3c4398d8c21a5b35b510fed Mon Sep 17 00:00:00 2001 From: Roman Varkuta Date: Thu, 25 Oct 2018 17:02:30 +0300 Subject: [PATCH 3/4] Add miss back slash --- src/Enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enum.php b/src/Enum.php index 957d3a8..1ee2573 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -177,7 +177,7 @@ public static function search($value) public static function __callStatic($name, $arguments) { $array = static::toArray(); - if (isset($array[$name]) || array_key_exists($name, $array)) { + if (isset($array[$name]) || \array_key_exists($name, $array)) { return new static($array[$name]); } From f8c4a112b96e597d9cde99ce90987045dce3a08b Mon Sep 17 00:00:00 2001 From: KartaviK Date: Thu, 25 Oct 2018 17:15:53 +0300 Subject: [PATCH 4/4] Add tests for boolean enum --- tests/EnumTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 96d09c7..4fd1000 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -269,4 +269,10 @@ public function testNullableEnum() $this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->getValue()); $this->assertNull((new EnumFixture(EnumFixture::PROBLEMATIC_NULL))->jsonSerialize()); } + + public function testBooleanEnum() + { + $this->assertFalse(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE()->getValue()); + $this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize()); + } }