From e2dcb079e0735afd664ae8fff37c70e7e150a4d2 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 13:44:48 -0500 Subject: [PATCH 1/8] Add getString and getBoolean --- src/Env.php | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Env.php b/src/Env.php index bf7a5f8..46ca0ba 100644 --- a/src/Env.php +++ b/src/Env.php @@ -2,7 +2,6 @@ namespace Sil\PhpEnv; use Exception; -use Sil\PhpEnv\EnvListNotAvailableException; class Env { @@ -78,7 +77,45 @@ public static function get($varname, $default = null) return $trimmedValue; } - + + public static function getString(string $varName): ?string + { + $originalValue = \getenv($varName); + if ($originalValue === false) { + return null; + } + $trimmedValue = \trim($originalValue); + return $trimmedValue; + } + + + public static function getBoolean(string $varName): ?bool + { + $originalValue = \getenv($varName); + if ($originalValue === false) { + return null; + } + $trimmedValue = \trim($originalValue); + $loweredValue = \strtolower($trimmedValue); + + $mapping = [ + 'no' => false, + 'false' => false, + '0' => false, + 'off' => false, + 'yes' => true, + 'true' => true, + '1' => true, + 'on' => true, + ]; + if (array_key_exists($loweredValue, $mapping)) { + $returnValue = $mapping[$loweredValue]; + } else { + $returnValue = null; + } + return $returnValue; + } + /** * * @param string $varname From 17f2f6abc469dd4624c6a6ddde516affb6efecf9 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 13:46:04 -0500 Subject: [PATCH 2/8] Update dependencies to PHP 7.4, PHPUnit 9.x, and 7.4 docker image --- composer.json | 4 ++-- docker-compose.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 651f157..5ca0108 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,9 @@ "psr-4": {"Sil\\PhpEnv\\": "src/"} }, "require": { - "php": "^7.2" + "php": "^7.4" }, "require-dev": { - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^9.0" } } diff --git a/docker-compose.yml b/docker-compose.yml index d1a512b..b66d24a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '2' services: test: - image: silintl/php7:7.2 + image: silintl/php7:7.4 volumes: - ./:/data working_dir: /data From 8f1a5b1160a7bd312bce9ec5b815b5eab9aae138 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 13:57:27 -0500 Subject: [PATCH 3/8] Make sure to update the scrutinizer PHP version --- .scrutinizer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 5f65666..a880c54 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -128,7 +128,7 @@ build_failure_conditions: build: environment: - php: 7.2.22 + php: 7.4.28 dependencies: before: From 18563650c84b0247f0e9bda4144f7657c6366502 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 14:26:30 -0500 Subject: [PATCH 4/8] Change getString and getBoolean to use get and then strong type --- src/Env.php | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/Env.php b/src/Env.php index 46ca0ba..775bb97 100644 --- a/src/Env.php +++ b/src/Env.php @@ -78,42 +78,29 @@ public static function get($varname, $default = null) return $trimmedValue; } - public static function getString(string $varName): ?string + public static function getString(string $varName, ?string $default = null): ?string { - $originalValue = \getenv($varName); - if ($originalValue === false) { + $value = self::get($varName, $default); + if ($value === null) { + return $value; + } elseif (is_string($value)) { + return $value; + } else { return null; } - $trimmedValue = \trim($originalValue); - return $trimmedValue; } - public static function getBoolean(string $varName): ?bool + public static function getBoolean(string $varName, ?bool $default = null): ?bool { - $originalValue = \getenv($varName); - if ($originalValue === false) { - return null; - } - $trimmedValue = \trim($originalValue); - $loweredValue = \strtolower($trimmedValue); - - $mapping = [ - 'no' => false, - 'false' => false, - '0' => false, - 'off' => false, - 'yes' => true, - 'true' => true, - '1' => true, - 'on' => true, - ]; - if (array_key_exists($loweredValue, $mapping)) { - $returnValue = $mapping[$loweredValue]; + $value = self::get($varName, $default); + if ($value === null) { + return $value; + } elseif (is_bool($value)) { + return $value; } else { - $returnValue = null; + return null; } - return $returnValue; } /** From 01fea6e07e1e6965e18f98a5181f707a52b380db Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 14:30:20 -0500 Subject: [PATCH 5/8] Simplify code paths --- src/Env.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Env.php b/src/Env.php index 775bb97..fa93947 100644 --- a/src/Env.php +++ b/src/Env.php @@ -81,9 +81,7 @@ public static function get($varname, $default = null) public static function getString(string $varName, ?string $default = null): ?string { $value = self::get($varName, $default); - if ($value === null) { - return $value; - } elseif (is_string($value)) { + if (is_string($value)) { return $value; } else { return null; @@ -94,9 +92,7 @@ public static function getString(string $varName, ?string $default = null): ?str public static function getBoolean(string $varName, ?bool $default = null): ?bool { $value = self::get($varName, $default); - if ($value === null) { - return $value; - } elseif (is_bool($value)) { + if (is_bool($value)) { return $value; } else { return null; From 972e86a2368f644be7695f65b7e7c98f5bcc2bfa Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 14:39:31 -0500 Subject: [PATCH 6/8] Reworking getString and getBoolean a bit more --- src/Env.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Env.php b/src/Env.php index fa93947..a641baa 100644 --- a/src/Env.php +++ b/src/Env.php @@ -80,12 +80,17 @@ public static function get($varname, $default = null) public static function getString(string $varName, ?string $default = null): ?string { - $value = self::get($varName, $default); - if (is_string($value)) { - return $value; - } else { - return null; + $originalValue = \getenv($varName); + if ($originalValue === false) { + return $default; } + + $trimmedValue = \trim($originalValue); + if ($trimmedValue === '') { + return $default; + } + + return $trimmedValue; } @@ -95,7 +100,7 @@ public static function getBoolean(string $varName, ?bool $default = null): ?bool if (is_bool($value)) { return $value; } else { - return null; + return $default; } } From f6ce152e08711646ca3b4fd4966283128c8ce417 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 14:46:36 -0500 Subject: [PATCH 7/8] Add documentation to README.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 5283dda..0f0108b 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,21 @@ php values of `true`, `false`, or `null` respectively * NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php `true`. `false` and `null` are handled similarly. Other value strings will have leading/trailing whitespace trimmed. +1. __getString__ - `public static function getString($varname, $default = null): ?string` + + * searches the local environment for `$varname` and returns the corresponding trimmed value string + * if `$varname` is not set or the value is empty (only whitespace), `getString` returns `$default` parameter + +1. __getBoolean__ - `public static function getBoolean($varname, $default = null): ?bool` + + * searches the local environment for `$varname` and returns the corresponding boolean value string + * if `$varname` is not set or the value is empty (only whitespace), `getBoolean` returns `$default` parameter + * if the value string corresponding to `$varname` is 'true', 'false' or 'null', `getBoolean` returns + php values of `true`, `false`, or `null` respectively + * if the value is not boolean, `getBoolean` returns `$default` parameter + * NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php `true`. + `false` and `null` are handled similarly. + 1. __getArray__ - `public static function getArray($varname, array $default = [])` * searches the local environment for `$varname` and returns the corresponding value string with comma separated elements as a php array From 684a60d54f886063af60566a5d17d57471aedce9 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 23 Feb 2022 14:47:39 -0500 Subject: [PATCH 8/8] Removed trailing whitespace --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f0108b..5709306 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ php values of `true`, `false`, or `null` respectively * if the value string corresponding to `$varname` is 'true', 'false' or 'null', `getBoolean` returns php values of `true`, `false`, or `null` respectively * if the value is not boolean, `getBoolean` returns `$default` parameter - * NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php `true`. + * NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php `true`. `false` and `null` are handled similarly. 1. __getArray__ - `public static function getArray($varname, array $default = [])`