Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vlucas/phpdotenv
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.5.0
Choose a base ref
...
head repository: vlucas/phpdotenv
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.6.0
Choose a head ref
  • 3 commits
  • 7 files changed
  • 2 contributors

Commits on Sep 4, 2019

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    4cd2eae View commit details

Commits on Sep 10, 2019

  1. Bumped version

    GrahamCampbell authored Sep 10, 2019

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    8e53d9a View commit details
  2. Added Validator::allowedRegexValues (#371)

    Co-authored-by: Chaim Paperman <chaim@treestone.com>
    GrahamCampbell and treestonemedia authored Sep 10, 2019

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    1bdf24f View commit details
Showing with 92 additions and 9 deletions.
  1. +5 −0 README.md
  2. +2 −2 composer.json
  3. +18 −3 src/Regex/Regex.php
  4. +1 −1 src/Regex/Result.php
  5. +3 −3 src/Regex/Success.php
  6. +25 −0 src/Validator.php
  7. +38 −0 tests/Dotenv/ValidatorTest.php
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
"symfony/polyfill-ctype": "^1.9"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0"
"phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
@@ -30,7 +30,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.5-dev"
"dev-master": "3.6-dev"
}
}
}
21 changes: 18 additions & 3 deletions src/Regex/Regex.php
Original file line number Diff line number Diff line change
@@ -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.
*
@@ -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);
}

@@ -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);
}

@@ -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));
2 changes: 1 addition & 1 deletion src/Regex/Result.php
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ abstract public function success();
/**
* Get the error value, if possible.
*
* @return string
* @return string|int
*/
public function getSuccess()
{
6 changes: 3 additions & 3 deletions src/Regex/Success.php
Original file line number Diff line number Diff line change
@@ -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
*/
@@ -27,7 +27,7 @@ private function __construct($value)
/**
* Create a new success value.
*
* @param string $value
* @param string|int $value
*
* @return \Dotenv\Regex\Result
*/
25 changes: 25 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
namespace Dotenv;

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

/**
* This is the validator class.
@@ -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.
*
38 changes: 38 additions & 0 deletions tests/Dotenv/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}