diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 52868ff..95c52fb 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -135,7 +135,7 @@ build: tests-and-coverage: environment: - php: 7.2.22 + php: 7.4.28 dependencies: before: diff --git a/README.md b/README.md index 5283dda..5709306 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 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 diff --git a/src/Env.php b/src/Env.php index bf7a5f8..a641baa 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,33 @@ public static function get($varname, $default = null) return $trimmedValue; } - + + public static function getString(string $varName, ?string $default = null): ?string + { + $originalValue = \getenv($varName); + if ($originalValue === false) { + return $default; + } + + $trimmedValue = \trim($originalValue); + if ($trimmedValue === '') { + return $default; + } + + return $trimmedValue; + } + + + public static function getBoolean(string $varName, ?bool $default = null): ?bool + { + $value = self::get($varName, $default); + if (is_bool($value)) { + return $value; + } else { + return $default; + } + } + /** * * @param string $varname