dependencies-upgrade
This commit is contained in:
132
vendor/vlucas/phpdotenv/src/Validator.php
vendored
132
vendor/vlucas/phpdotenv/src/Validator.php
vendored
@@ -1,10 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dotenv;
|
||||
|
||||
use Dotenv\Exception\ValidationException;
|
||||
use Dotenv\Regex\Regex;
|
||||
use Dotenv\Repository\RepositoryInterface;
|
||||
use Dotenv\Util\Regex;
|
||||
use Dotenv\Util\Str;
|
||||
|
||||
class Validator
|
||||
{
|
||||
@@ -13,39 +16,46 @@ class Validator
|
||||
*
|
||||
* @var \Dotenv\Repository\RepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* The variables to validate.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $variables;
|
||||
private $variables;
|
||||
|
||||
/**
|
||||
* Create a new validator instance.
|
||||
*
|
||||
* @param \Dotenv\Repository\RepositoryInterface $repository
|
||||
* @param string[] $variables
|
||||
* @param bool $required
|
||||
*
|
||||
* @throws \Dotenv\Exception\ValidationException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(RepositoryInterface $repository, array $variables, $required = true)
|
||||
public function __construct(RepositoryInterface $repository, array $variables)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->variables = $variables;
|
||||
}
|
||||
|
||||
if ($required) {
|
||||
$this->assertCallback(
|
||||
function ($value) {
|
||||
return $value !== null;
|
||||
},
|
||||
'is missing'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Assert that each variable is present.
|
||||
*
|
||||
* @throws \Dotenv\Exception\ValidationException
|
||||
*
|
||||
* @return \Dotenv\Validator
|
||||
*/
|
||||
public function required()
|
||||
{
|
||||
return $this->assert(
|
||||
static function (?string $value) {
|
||||
return $value !== null;
|
||||
},
|
||||
'is missing'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,13 +67,9 @@ class Validator
|
||||
*/
|
||||
public function notEmpty()
|
||||
{
|
||||
return $this->assertCallback(
|
||||
function ($value) {
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return strlen(trim($value)) > 0;
|
||||
return $this->assertNullable(
|
||||
static function (string $value) {
|
||||
return Str::len(\trim($value)) > 0;
|
||||
},
|
||||
'is empty'
|
||||
);
|
||||
@@ -78,13 +84,9 @@ class Validator
|
||||
*/
|
||||
public function isInteger()
|
||||
{
|
||||
return $this->assertCallback(
|
||||
function ($value) {
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ctype_digit($value);
|
||||
return $this->assertNullable(
|
||||
static function (string $value) {
|
||||
return \ctype_digit($value);
|
||||
},
|
||||
'is not an integer'
|
||||
);
|
||||
@@ -99,17 +101,13 @@ class Validator
|
||||
*/
|
||||
public function isBoolean()
|
||||
{
|
||||
return $this->assertCallback(
|
||||
function ($value) {
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->assertNullable(
|
||||
static function (string $value) {
|
||||
if ($value === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null;
|
||||
return \filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE) !== null;
|
||||
},
|
||||
'is not a boolean'
|
||||
);
|
||||
@@ -126,15 +124,11 @@ class Validator
|
||||
*/
|
||||
public function allowedValues(array $choices)
|
||||
{
|
||||
return $this->assertCallback(
|
||||
function ($value) use ($choices) {
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array($value, $choices, true);
|
||||
return $this->assertNullable(
|
||||
static function (string $value) use ($choices) {
|
||||
return \in_array($value, $choices, true);
|
||||
},
|
||||
sprintf('is not one of [%s]', implode(', ', $choices))
|
||||
\sprintf('is not one of [%s]', \implode(', ', $choices))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -147,47 +141,69 @@ class Validator
|
||||
*
|
||||
* @return \Dotenv\Validator
|
||||
*/
|
||||
public function allowedRegexValues($regex)
|
||||
public function allowedRegexValues(string $regex)
|
||||
{
|
||||
return $this->assertCallback(
|
||||
function ($value) use ($regex) {
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return Regex::match($regex, $value)->success()->getOrElse(0) === 1;
|
||||
return $this->assertNullable(
|
||||
static function (string $value) use ($regex) {
|
||||
return Regex::matches($regex, $value)->success()->getOrElse(false);
|
||||
},
|
||||
sprintf('does not match "%s"', $regex)
|
||||
\sprintf('does not match "%s"', $regex)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the callback returns true for each variable.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @param callable(?string):bool $callback
|
||||
* @param string $message
|
||||
*
|
||||
* @throws \Dotenv\Exception\ValidationException
|
||||
*
|
||||
* @return \Dotenv\Validator
|
||||
*/
|
||||
protected function assertCallback(callable $callback, $message = 'failed callback assertion')
|
||||
public function assert(callable $callback, string $message)
|
||||
{
|
||||
$failing = [];
|
||||
|
||||
foreach ($this->variables as $variable) {
|
||||
if ($callback($this->repository->get($variable)) === false) {
|
||||
$failing[] = sprintf('%s %s', $variable, $message);
|
||||
$failing[] = \sprintf('%s %s', $variable, $message);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($failing) > 0) {
|
||||
throw new ValidationException(sprintf(
|
||||
if (\count($failing) > 0) {
|
||||
throw new ValidationException(\sprintf(
|
||||
'One or more environment variables failed assertions: %s.',
|
||||
implode(', ', $failing)
|
||||
\implode(', ', $failing)
|
||||
));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the callback returns true for each variable.
|
||||
*
|
||||
* Skip checking null variable values.
|
||||
*
|
||||
* @param callable(string):bool $callback
|
||||
* @param string $message
|
||||
*
|
||||
* @throws \Dotenv\Exception\ValidationException
|
||||
*
|
||||
* @return \Dotenv\Validator
|
||||
*/
|
||||
public function assertNullable(callable $callback, string $message)
|
||||
{
|
||||
return $this->assert(
|
||||
static function (?string $value) use ($callback) {
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $callback($value);
|
||||
},
|
||||
$message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user