Skip to content

Commit fce9a5f

Browse files
BruceDevGrahamCampbell
authored andcommitted
Added integer validation
Closes #118
1 parent be2a943 commit fce9a5f

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
@@ -175,6 +175,20 @@ If the environment variable is empty, you'd get an Exception:
175175
One or more environment variables failed assertions: DATABASE_DSN is empty
176176
```
177177

178+
### Integer Variables
179+
180+
You might also need to ensure the the variable is of an integer value. You may do the following:
181+
182+
```php
183+
$dotenv->required('FOO')->isInteger();
184+
```
185+
186+
If the environment variable is not an integer, you'd get an Exception:
187+
188+
```
189+
One or more environment variables failed assertions: FOO is not an integer
190+
```
191+
178192
### Allowed Values
179193

180194
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
@@ -62,6 +62,21 @@ function ($value) {
6262
);
6363
}
6464

65+
/**
66+
* Assert that each specified variable is a number.
67+
*
68+
* @return \Dotenv\Validator
69+
*/
70+
public function isInteger()
71+
{
72+
return $this->assertCallback(
73+
function ($value) {
74+
return ctype_digit($value);
75+
},
76+
'is not an integer'
77+
);
78+
}
79+
6580
/**
6681
* Assert that each variable is amongst the given choices.
6782
*

0 commit comments

Comments
 (0)