From fba64139db67123c7a57072e5f8d3db10d160b66 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 14 Sep 2020 16:57:31 +0100 Subject: [PATCH] Support boolean values in $_ENV and $_SERVER (#457) --- src/Repository/Adapter/EnvConstAdapter.php | 17 ++++++++++++++--- src/Repository/Adapter/ServerConstAdapter.php | 17 ++++++++++++++--- .../Repository/Adapter/EnvConstAdapterTest.php | 16 ++++++++++++++++ .../Adapter/ServerConstAdapterTest.php | 16 ++++++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/Repository/Adapter/EnvConstAdapter.php b/src/Repository/Adapter/EnvConstAdapter.php index c2b7af84..9ef7fb4d 100644 --- a/src/Repository/Adapter/EnvConstAdapter.php +++ b/src/Repository/Adapter/EnvConstAdapter.php @@ -40,9 +40,20 @@ public static function create() public function read(string $name) { /** @var \PhpOption\Option */ - return Option::fromArraysValue($_ENV, $name)->filter(static function ($value) { - return \is_string($value); - }); + return Option::fromArraysValue($_ENV, $name) + ->map(static function ($value) { + if ($value === false) { + return 'false'; + } + + if ($value === true) { + return 'true'; + } + + return $value; + })->filter(static function ($value) { + return \is_string($value); + }); } /** diff --git a/src/Repository/Adapter/ServerConstAdapter.php b/src/Repository/Adapter/ServerConstAdapter.php index d43552d6..8e3dc98e 100644 --- a/src/Repository/Adapter/ServerConstAdapter.php +++ b/src/Repository/Adapter/ServerConstAdapter.php @@ -40,9 +40,20 @@ public static function create() public function read(string $name) { /** @var \PhpOption\Option */ - return Option::fromArraysValue($_SERVER, $name)->filter(static function ($value) { - return \is_string($value); - }); + return Option::fromArraysValue($_SERVER, $name) + ->map(static function ($value) { + if ($value === false) { + return 'false'; + } + + if ($value === true) { + return 'true'; + } + + return $value; + })->filter(static function ($value) { + return \is_string($value); + }); } /** diff --git a/tests/Dotenv/Repository/Adapter/EnvConstAdapterTest.php b/tests/Dotenv/Repository/Adapter/EnvConstAdapterTest.php index bea69e9b..592f304e 100644 --- a/tests/Dotenv/Repository/Adapter/EnvConstAdapterTest.php +++ b/tests/Dotenv/Repository/Adapter/EnvConstAdapterTest.php @@ -17,6 +17,22 @@ public function testGoodRead() self::assertSame('foo bar baz', $value->get()); } + public function testFalseRead() + { + $_ENV['CONST_TEST'] = false; + $value = self::createAdapter()->read('CONST_TEST'); + self::assertTrue($value->isDefined()); + self::assertSame('false', $value->get()); + } + + public function testTrueRead() + { + $_ENV['CONST_TEST'] = true; + $value = self::createAdapter()->read('CONST_TEST'); + self::assertTrue($value->isDefined()); + self::assertSame('true', $value->get()); + } + public function testBadTypeRead() { $_ENV['CONST_TEST'] = 123; diff --git a/tests/Dotenv/Repository/Adapter/ServerConstAdapterTest.php b/tests/Dotenv/Repository/Adapter/ServerConstAdapterTest.php index 98452d8b..7366b79a 100644 --- a/tests/Dotenv/Repository/Adapter/ServerConstAdapterTest.php +++ b/tests/Dotenv/Repository/Adapter/ServerConstAdapterTest.php @@ -17,6 +17,22 @@ public function testGoodRead() self::assertSame('foo bar baz', $value->get()); } + public function testFalseRead() + { + $_SERVER['CONST_TEST'] = false; + $value = self::createAdapter()->read('CONST_TEST'); + self::assertTrue($value->isDefined()); + self::assertSame('false', $value->get()); + } + + public function testTrueRead() + { + $_SERVER['CONST_TEST'] = true; + $value = self::createAdapter()->read('CONST_TEST'); + self::assertTrue($value->isDefined()); + self::assertSame('true', $value->get()); + } + public function testBadTypeRead() { $_SERVER['CONST_TEST'] = 123;