Skip to content

Commit b755acf

Browse files
author
sephedo
committed
Added the Valiator method isBoolean
1 parent 3cc116a commit b755acf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ If the environment variable is not an integer, you'd get an Exception:
189189
One or more environment variables failed assertions: FOO is not an integer
190190
```
191191

192+
### Boolean Variables
193+
194+
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:
195+
196+
```php
197+
$dotenv->required('FOO')->isBoolean();
198+
```
199+
200+
If the environment variable is not a boolean, you'd get an Exception:
201+
202+
```
203+
One or more environment variables failed assertions: FOO is not a boolean
204+
```
205+
192206
### Allowed Values
193207

194208
It is also possible to define a set of values that your environment variable

src/Validator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ function ($value) {
7676
'is not an integer'
7777
);
7878
}
79+
80+
/**
81+
* Asset that each specified variable is a boolean.
82+
*
83+
* @return \Dotenv\Validator
84+
*/
85+
public function isBoolean()
86+
{
87+
return $this->assertCallback(
88+
function($value) {
89+
return (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== NULL);
90+
},
91+
'is not a boolean'
92+
);
93+
}
7994

8095
/**
8196
* Assert that each variable is amongst the given choices.

0 commit comments

Comments
 (0)