Skip to content

Commit

Permalink
Support boolean values in $_ENV and $_SERVER (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored Sep 14, 2020
1 parent 3af4965 commit fba6413
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/Repository/Adapter/EnvConstAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ public static function create()
public function read(string $name)
{
/** @var \PhpOption\Option<string> */
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);
});
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/Repository/Adapter/ServerConstAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ public static function create()
public function read(string $name)
{
/** @var \PhpOption\Option<string> */
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);
});
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Dotenv/Repository/Adapter/EnvConstAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/Dotenv/Repository/Adapter/ServerConstAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fba6413

Please sign in to comment.