diff --git a/README.md b/README.md index 9f1bb23c..2fbc5889 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,20 @@ If the environment variable is not an integer, you'd get an Exception: One or more environment variables failed assertions: FOO is not an integer ``` +### Boolean Variables + +You may need to ensure a variable is in the form of a boolean, accepting "On", "1", "Yes", "Off", "0" and "No". You may do the following: + +```php +$dotenv->required('FOO')->isBoolean(); +``` + +If the environment variable is not a boolean, you'd get an Exception: + +``` +One or more environment variables failed assertions: FOO is not a boolean +``` + ### Allowed Values It is also possible to define a set of values that your environment variable diff --git a/src/Validator.php b/src/Validator.php index aa3cb0e4..53a7fd5a 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -77,6 +77,25 @@ function ($value) { ); } + /** + * Assert that each specified variable is a boolean. + * + * @return \Dotenv\Validator + */ + public function isBoolean() + { + return $this->assertCallback( + function ($value) { + if ($value === '') { + return false; + } + + return (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== NULL); + }, + 'is not a boolean' + ); + } + /** * Assert that each variable is amongst the given choices. * diff --git a/tests/Dotenv/ValidatorBooleanTest.php b/tests/Dotenv/ValidatorBooleanTest.php new file mode 100644 index 00000000..14bb8ec7 --- /dev/null +++ b/tests/Dotenv/ValidatorBooleanTest.php @@ -0,0 +1,95 @@ +fixturesFolder = dirname(__DIR__).'/fixtures/env'; + } + + /** + * List of valid boolean values in fixtures/env/booleans.env + * + * @return array + */ + public function validBooleanValuesDataProvider() + { + return array( + array('VALID_EXPLICIT_LOWERCASE_TRUE'), + array('VALID_EXPLICIT_LOWERCASE_FALSE'), + array('VALID_EXPLICIT_UPPERCASE_TRUE'), + array('VALID_EXPLICIT_UPPERCASE_FALSE'), + array('VALID_EXPLICIT_MIXEDCASE_TRUE'), + array('VALID_EXPLICIT_MIXEDCASE_FALSE'), + + array('VALID_NUMBER_TRUE'), + array('VALID_NUMBER_FALSE'), + + array('VALID_ONOFF_LOWERCASE_TRUE'), + array('VALID_ONOFF_LOWERCASE_FALSE'), + array('VALID_ONOFF_UPPERCASE_TRUE'), + array('VALID_ONOFF_UPPERCASE_FALSE'), + array('VALID_ONOFF_MIXEDCASE_TRUE'), + array('VALID_ONOFF_MIXEDCASE_FALSE'), + + array('VALID_YESNO_LOWERCASE_TRUE'), + array('VALID_YESNO_LOWERCASE_FALSE'), + array('VALID_YESNO_UPPERCASE_TRUE'), + array('VALID_YESNO_UPPERCASE_FALSE'), + array('VALID_YESNO_MIXEDCASE_TRUE'), + array('VALID_YESNO_MIXEDCASE_FALSE'), + ); + } + + /** + * @dataProvider validBooleanValuesDataProvider + */ + public function testCanValidateBooleans($boolean) + { + $dotenv = new Dotenv($this->fixturesFolder, 'booleans.env'); + $dotenv->load(); + + $dotenv->required($boolean)->isBoolean(); + + $this->assertTrue(true); // anything wrong - an exception will be thrown + } + + /** + * List of non-boolean values in fixtures/env/booleans.env + * + * @return array + */ + public function invalidBooleanValuesDataProvider() + { + return array( + array('INVALID_SOMETHING'), + array('INVALID_EMPTY'), + array('INVALID_EMPTY_STRING'), + array('INVALID_NULL'), + array('INVALID_NUMBER_POSITIVE'), + array('INVALID_NUMBER_NEGATIVE'), + array('INVALID_MINUS'), + array('INVALID_TILDA'), + array('INVALID_EXCLAMATION'), + ); + } + + /** + * @dataProvider invalidBooleanValuesDataProvider + * @expectedException Dotenv\Exception\ValidationException + */ + public function testCanInvalidateNonBooleans($boolean) + { + $dotenv = new Dotenv($this->fixturesFolder, 'booleans.env'); + $dotenv->load(); + + $dotenv->required($boolean)->isBoolean(); + } +} diff --git a/tests/fixtures/env/booleans.env b/tests/fixtures/env/booleans.env new file mode 100644 index 00000000..522ac456 --- /dev/null +++ b/tests/fixtures/env/booleans.env @@ -0,0 +1,33 @@ +VALID_EXPLICIT_LOWERCASE_TRUE=true +VALID_EXPLICIT_LOWERCASE_FALSE=false +VALID_EXPLICIT_UPPERCASE_TRUE=TRUE +VALID_EXPLICIT_UPPERCASE_FALSE=FALSE +VALID_EXPLICIT_MIXEDCASE_TRUE=True +VALID_EXPLICIT_MIXEDCASE_FALSE=False + +VALID_NUMBER_TRUE=1 +VALID_NUMBER_FALSE=0 + +VALID_ONOFF_LOWERCASE_TRUE=on +VALID_ONOFF_LOWERCASE_FALSE=off +VALID_ONOFF_UPPERCASE_TRUE=ON +VALID_ONOFF_UPPERCASE_FALSE=OFF +VALID_ONOFF_MIXEDCASE_TRUE=On +VALID_ONOFF_MIXEDCASE_FALSE=Off + +VALID_YESNO_LOWERCASE_TRUE=yes +VALID_YESNO_LOWERCASE_FALSE=no +VALID_YESNO_UPPERCASE_TRUE=YES +VALID_YESNO_UPPERCASE_FALSE=NO +VALID_YESNO_MIXEDCASE_TRUE=Yes +VALID_YESNO_MIXEDCASE_FALSE=No + +INVALID_SOMETHING=something +INVALID_EMPTY= +INVALID_EMPTY_STRING="" +INVALID_NULL=null +INVALID_NUMBER_POSITIVE=2 +INVALID_NUMBER_NEGATIVE=-2 +INVALID_MINUS=- +INVALID_TILDA=~ +INVALID_EXCLAMATION=!