Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ build:

tests-and-coverage:
environment:
php: 7.2.22
php: 7.4.28

dependencies:
before:
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
test:
image: silintl/php7:7.2
image: silintl/php7:7.4
volumes:
- ./:/data
working_dir: /data
Expand Down
29 changes: 27 additions & 2 deletions src/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Sil\PhpEnv;

use Exception;
use Sil\PhpEnv\EnvListNotAvailableException;

class Env
{
Expand Down Expand Up @@ -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
Expand Down