Skip to content

Commit

Permalink
Allow setting values to booleans (#58)
Browse files Browse the repository at this point in the history
* Allow setting values to booleans

* Fix StyleCI config

* Apply fixes from StyleCI (#59)

Co-authored-by: StyleCI Bot <bot@styleci.io>

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
svenluijten and StyleCIBot committed Dec 21, 2022
1 parent 389fc14 commit 4cc20bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
4 changes: 0 additions & 4 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ preset: laravel

risky: false

enabled:
- phpdoc_order
- phpdoc_separation

disabled:
- not_operator_with_successor_space
33 changes: 16 additions & 17 deletions src/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Env
/**
* Instantiate the Env.
*
* @param string $path
* @param string $path
*/
public function __construct($path)
{
Expand All @@ -39,8 +39,7 @@ public function __construct($path)
/**
* Get an entry from the .env file by key.
*
* @param string $key
*
* @param string $key
* @return string
*/
public function get($key)
Expand All @@ -57,17 +56,20 @@ public function get($key)
/**
* Set the value of the given key to the value supplied.
*
* @param string $key
* @param string $value
* @param bool $linebreak
*
* @param string $key
* @param string|bool|int $value
* @param bool $linebreak
* @return \Sven\FlexEnv\Env
*/
public function set($key, $value, $linebreak = false)
{
$oldValue = $this->get($key);

if (!preg_match('/\d/', $value) || preg_match('/=/', $value)) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

if (preg_match('/\W\D/', $value)) {
$value = "\"$value\"";
}

Expand All @@ -85,8 +87,7 @@ public function set($key, $value, $linebreak = false)
/**
* Delete an entry from the .env file.
*
* @param string $key
*
* @param string $key
* @return \Sven\FlexEnv\Env
*/
public function delete($key)
Expand Down Expand Up @@ -118,9 +119,8 @@ public function all()
/**
* Copy the .env file to the given destination.
*
* @param string $destination Full path to copy the file to
* @param bool $excludeValues Whether or not to include values
*
* @param string $destination Full path to copy the file to
* @param bool $excludeValues Whether or not to include values
* @return bool
*/
public function copy($destination, $excludeValues = false)
Expand Down Expand Up @@ -178,10 +178,9 @@ private function parseFile()
/**
* Replace a part of the .env file.
*
* @param string $old
* @param string $new
* @param int $append
*
* @param string $old
* @param string $new
* @param int $append
* @return \Sven\FlexEnv\Env
*/
public function replaceInFile($old, $new, $append = 0)
Expand Down
15 changes: 12 additions & 3 deletions tests/EnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public function it_can_set_values()
);
}

/** @test */
public function it_can_set_boolean_values()
{
$result = $this->flex->set('BOOLEAN_VALUE', true)
->get('BOOLEAN_VALUE');

$this->assertEquals('true', $result);
}

/** @test */
public function it_removes_an_entry()
{
Expand Down Expand Up @@ -91,7 +100,7 @@ public function it_can_list_values_with_spaces()
->set('VARIABLE', 'variable');

$this->assertEquals(
['HELLO_WORLD' => '"hello world"', 'TEST_VARIABLE' => '"test variable"', 'VARIABLE' => '"variable"'],
['HELLO_WORLD' => '"hello world"', 'TEST_VARIABLE' => '"test variable"', 'VARIABLE' => 'variable'],
$this->flex->all()
);
}
Expand All @@ -104,13 +113,13 @@ public function it_can_set_values_with_special_characters()
->set('MAIL_ENCRYPTION', 'tls');

$this->assertEquals(
['MAIL_USERNAME' => '"name@example.com"', 'MAIL_PASSWORD' => '"==hy6^gGbS+=="', 'MAIL_ENCRYPTION' => '"tls"'],
['MAIL_USERNAME' => '"name@example.com"', 'MAIL_PASSWORD' => '"==hy6^gGbS+=="', 'MAIL_ENCRYPTION' => 'tls'],
$this->flex->all()
);

$this->assertEquals('
MAIL_USERNAME="name@example.com"
MAIL_PASSWORD="==hy6^gGbS+=="
MAIL_ENCRYPTION="tls"', file_get_contents(__DIR__.'/assets/.env'));
MAIL_ENCRYPTION=tls', file_get_contents(__DIR__.'/assets/.env'));
}
}

0 comments on commit 4cc20bf

Please sign in to comment.