diff --git a/CHANGELOG.md b/CHANGELOG.md index 69597ac..f61f8cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +3.4.1 +===== + +* (improvement) Normalize empty strings in `ImportData` to `null`. + + 3.4.0 ===== diff --git a/src/Import/ImportData.php b/src/Import/ImportData.php index 5e7fb88..25bdaf0 100644 --- a/src/Import/ImportData.php +++ b/src/Import/ImportData.php @@ -72,7 +72,11 @@ public function getOptionalString (string $path) : ?string )); } - return (string) $value; + $result = (string) $value; + + return "" !== $result + ? $result + : null; } /** diff --git a/tests/Import/ImportDataTest.php b/tests/Import/ImportDataTest.php index a5881a6..25cc147 100644 --- a/tests/Import/ImportDataTest.php +++ b/tests/Import/ImportDataTest.php @@ -25,6 +25,7 @@ public function testValid () : void "bool" => true, "string" => "text", "enum" => "test", + "empty-string" => "", "null" => null, "nested" => [ "a" => 15, @@ -49,6 +50,7 @@ public function testValid () : void self::assertSame("2", $data->getString("int")); self::assertSame("2.5", $data->getString("float")); self::assertSame("1", $data->getString("bool")); + self::assertNull($data->getOptionalString("empty-string")); // enum self::assertSame(ExampleBackedEnum::Test, $data->getEnum("enum", ExampleBackedEnum::class));