Skip to content

Commit 90b2f41

Browse files
authored
Merge pull request #197 from sephedo/master
Added the Valiator method isBoolean
2 parents ec37c49 + f467b72 commit 90b2f41

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

README.md

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

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

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

src/Validator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ function ($value) {
7777
);
7878
}
7979

80+
/**
81+
* Assert 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+
if ($value === '') {
90+
return false;
91+
}
92+
93+
return (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== NULL);
94+
},
95+
'is not a boolean'
96+
);
97+
}
98+
8099
/**
81100
* Assert that each variable is amongst the given choices.
82101
*

tests/Dotenv/ValidatorBooleanTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
use Dotenv\Dotenv;
4+
5+
class ValidatorBooleanTest extends PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $fixturesFolder;
11+
12+
public function setUp()
13+
{
14+
$this->fixturesFolder = dirname(__DIR__).'/fixtures/env';
15+
}
16+
17+
/**
18+
* List of valid boolean values in fixtures/env/booleans.env
19+
*
20+
* @return array
21+
*/
22+
public function validBooleanValuesDataProvider()
23+
{
24+
return array(
25+
array('VALID_EXPLICIT_LOWERCASE_TRUE'),
26+
array('VALID_EXPLICIT_LOWERCASE_FALSE'),
27+
array('VALID_EXPLICIT_UPPERCASE_TRUE'),
28+
array('VALID_EXPLICIT_UPPERCASE_FALSE'),
29+
array('VALID_EXPLICIT_MIXEDCASE_TRUE'),
30+
array('VALID_EXPLICIT_MIXEDCASE_FALSE'),
31+
32+
array('VALID_NUMBER_TRUE'),
33+
array('VALID_NUMBER_FALSE'),
34+
35+
array('VALID_ONOFF_LOWERCASE_TRUE'),
36+
array('VALID_ONOFF_LOWERCASE_FALSE'),
37+
array('VALID_ONOFF_UPPERCASE_TRUE'),
38+
array('VALID_ONOFF_UPPERCASE_FALSE'),
39+
array('VALID_ONOFF_MIXEDCASE_TRUE'),
40+
array('VALID_ONOFF_MIXEDCASE_FALSE'),
41+
42+
array('VALID_YESNO_LOWERCASE_TRUE'),
43+
array('VALID_YESNO_LOWERCASE_FALSE'),
44+
array('VALID_YESNO_UPPERCASE_TRUE'),
45+
array('VALID_YESNO_UPPERCASE_FALSE'),
46+
array('VALID_YESNO_MIXEDCASE_TRUE'),
47+
array('VALID_YESNO_MIXEDCASE_FALSE'),
48+
);
49+
}
50+
51+
/**
52+
* @dataProvider validBooleanValuesDataProvider
53+
*/
54+
public function testCanValidateBooleans($boolean)
55+
{
56+
$dotenv = new Dotenv($this->fixturesFolder, 'booleans.env');
57+
$dotenv->load();
58+
59+
$dotenv->required($boolean)->isBoolean();
60+
61+
$this->assertTrue(true); // anything wrong - an exception will be thrown
62+
}
63+
64+
/**
65+
* List of non-boolean values in fixtures/env/booleans.env
66+
*
67+
* @return array
68+
*/
69+
public function invalidBooleanValuesDataProvider()
70+
{
71+
return array(
72+
array('INVALID_SOMETHING'),
73+
array('INVALID_EMPTY'),
74+
array('INVALID_EMPTY_STRING'),
75+
array('INVALID_NULL'),
76+
array('INVALID_NUMBER_POSITIVE'),
77+
array('INVALID_NUMBER_NEGATIVE'),
78+
array('INVALID_MINUS'),
79+
array('INVALID_TILDA'),
80+
array('INVALID_EXCLAMATION'),
81+
);
82+
}
83+
84+
/**
85+
* @dataProvider invalidBooleanValuesDataProvider
86+
* @expectedException Dotenv\Exception\ValidationException
87+
*/
88+
public function testCanInvalidateNonBooleans($boolean)
89+
{
90+
$dotenv = new Dotenv($this->fixturesFolder, 'booleans.env');
91+
$dotenv->load();
92+
93+
$dotenv->required($boolean)->isBoolean();
94+
}
95+
}

tests/fixtures/env/booleans.env

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
VALID_EXPLICIT_LOWERCASE_TRUE=true
2+
VALID_EXPLICIT_LOWERCASE_FALSE=false
3+
VALID_EXPLICIT_UPPERCASE_TRUE=TRUE
4+
VALID_EXPLICIT_UPPERCASE_FALSE=FALSE
5+
VALID_EXPLICIT_MIXEDCASE_TRUE=True
6+
VALID_EXPLICIT_MIXEDCASE_FALSE=False
7+
8+
VALID_NUMBER_TRUE=1
9+
VALID_NUMBER_FALSE=0
10+
11+
VALID_ONOFF_LOWERCASE_TRUE=on
12+
VALID_ONOFF_LOWERCASE_FALSE=off
13+
VALID_ONOFF_UPPERCASE_TRUE=ON
14+
VALID_ONOFF_UPPERCASE_FALSE=OFF
15+
VALID_ONOFF_MIXEDCASE_TRUE=On
16+
VALID_ONOFF_MIXEDCASE_FALSE=Off
17+
18+
VALID_YESNO_LOWERCASE_TRUE=yes
19+
VALID_YESNO_LOWERCASE_FALSE=no
20+
VALID_YESNO_UPPERCASE_TRUE=YES
21+
VALID_YESNO_UPPERCASE_FALSE=NO
22+
VALID_YESNO_MIXEDCASE_TRUE=Yes
23+
VALID_YESNO_MIXEDCASE_FALSE=No
24+
25+
INVALID_SOMETHING=something
26+
INVALID_EMPTY=
27+
INVALID_EMPTY_STRING=""
28+
INVALID_NULL=null
29+
INVALID_NUMBER_POSITIVE=2
30+
INVALID_NUMBER_NEGATIVE=-2
31+
INVALID_MINUS=-
32+
INVALID_TILDA=~
33+
INVALID_EXCLAMATION=!

0 commit comments

Comments
 (0)