Skip to content

Commit

Permalink
Added Validator::allowedRegexValues (#371)
Browse files Browse the repository at this point in the history
Co-authored-by: Chaim Paperman <[email protected]>
  • Loading branch information
GrahamCampbell and treestonemedia authored Sep 10, 2019
1 parent 8e53d9a commit 1bdf24f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ One or more environment variables failed assertions: SESSION_STORE is not an
allowed value
```

It is also possible to define a regex that your environment variable should be.
```php
$dotenv->required('FOO')->allowedRegexValues('([[:lower:]]{3})');
```

### Comments

You can comment your `.env` file using the `#` character. E.g.
Expand Down
21 changes: 18 additions & 3 deletions src/Regex/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

class Regex
{
/**
* Perform a preg match, wrapping up the result.
*
* @param string $pattern
* @param string $subject
*
* @return \Dotenv\Regex\Result
*/
public static function match($pattern, $subject)
{
return self::pregAndWrap(function ($subject) use ($pattern) {
return (int) @preg_match($pattern, $subject);
}, $subject);
}

/**
* Perform a preg replace, wrapping up the result.
*
Expand All @@ -18,7 +33,7 @@ class Regex
public static function replace($pattern, $replacement, $subject)
{
return self::pregAndWrap(function ($subject) use ($pattern, $replacement) {
return preg_replace($pattern, $replacement, $subject);
return (string) @preg_replace($pattern, $replacement, $subject);
}, $subject);
}

Expand All @@ -34,7 +49,7 @@ public static function replace($pattern, $replacement, $subject)
public static function replaceCallback($pattern, callable $callback, $subject)
{
return self::pregAndWrap(function ($subject) use ($pattern, $callback) {
return preg_replace_callback($pattern, $callback, $subject);
return (string) @preg_replace_callback($pattern, $callback, $subject);
}, $subject);
}

Expand All @@ -48,7 +63,7 @@ public static function replaceCallback($pattern, callable $callback, $subject)
*/
private static function pregAndWrap(callable $operation, $subject)
{
$result = (string) @$operation($subject);
$result = $operation($subject);

if (($e = preg_last_error()) !== PREG_NO_ERROR) {
return Error::create(self::lookupError($e));
Expand Down
2 changes: 1 addition & 1 deletion src/Regex/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract public function success();
/**
* Get the error value, if possible.
*
* @return string
* @return string|int
*/
public function getSuccess()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Regex/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
class Success extends Result
{
/**
* @var string
* @var string|int
*/
private $value;

/**
* Internal constructor for a success value.
*
* @param string $value
* @param string|int $value
*
* @return void
*/
Expand All @@ -27,7 +27,7 @@ private function __construct($value)
/**
* Create a new success value.
*
* @param string $value
* @param string|int $value
*
* @return \Dotenv\Regex\Result
*/
Expand Down
25 changes: 25 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dotenv;

use Dotenv\Exception\ValidationException;
use Dotenv\Regex\Regex;

/**
* This is the validator class.
Expand Down Expand Up @@ -138,6 +139,30 @@ function ($value) use ($choices) {
);
}

/**
* Assert that each variable matches the given regular expression.
*
* @param string $regex
*
* @throws \Dotenv\Exception\ValidationException
*
* @return \Dotenv\Validator
*/
public function allowedRegexValues($regex)
{
return $this->assertCallback(
function ($value) use ($regex)
{
if ($value === null) {
return true;
}

return Regex::match($regex, $value)->success()->getOrElse(0) === 1;
},
sprintf('does not match "%s"' , $regex)
);
}

/**
* Assert that the callback returns true for each variable.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Dotenv/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,42 @@ public function testIfPresentIntegerNonExist()
$dotenv->ifPresent(['VAR_DOES_NOT_EXIST_234782462764'])->isInteger();
$this->assertTrue(true);
}

public function testDotenvRegexMatchPass()
{
$dotenv = Dotenv::create($this->fixturesFolder);
$dotenv->load();
$dotenv->required('FOO')->allowedRegexValues('([[:lower:]]{3})');
$this->assertTrue(true);
}

/**
* @expectedException \Dotenv\Exception\ValidationException
* @expectedExceptionMessage One or more environment variables failed assertions: FOO does not match "/^([[:lower:]]{1})$/".
*/
public function testDotenvRegexMatchFail()
{
$dotenv = Dotenv::create($this->fixturesFolder);
$dotenv->load();
$dotenv->required('FOO')->allowedRegexValues('/^([[:lower:]]{1})$/');
}

/**
* @expectedException \Dotenv\Exception\ValidationException
* @expectedExceptionMessage One or more environment variables failed assertions: FOO does not match "/([[:lower:]{1{".
*/
public function testDotenvRegexMatchError()
{
$dotenv = Dotenv::create($this->fixturesFolder);
$dotenv->load();
$dotenv->required('FOO')->allowedRegexValues('/([[:lower:]{1{');
}

public function testDotenvRegexMatchNotPresent()
{
$dotenv = Dotenv::create($this->fixturesFolder);
$dotenv->load();
$dotenv->ifPresent('FOOOOOOOOOOO')->allowedRegexValues('([[:lower:]]{3})');
$this->assertTrue(true);
}
}

0 comments on commit 1bdf24f

Please sign in to comment.