Validator.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv;
  4. use Dotenv\Exception\ValidationException;
  5. use Dotenv\Repository\RepositoryInterface;
  6. use Dotenv\Util\Regex;
  7. use Dotenv\Util\Str;
  8. class Validator
  9. {
  10. /**
  11. * The environment repository instance.
  12. *
  13. * @var \Dotenv\Repository\RepositoryInterface
  14. */
  15. private $repository;
  16. /**
  17. * The variables to validate.
  18. *
  19. * @var string[]
  20. */
  21. private $variables;
  22. /**
  23. * Create a new validator instance.
  24. *
  25. * @param \Dotenv\Repository\RepositoryInterface $repository
  26. * @param string[] $variables
  27. *
  28. * @return void
  29. */
  30. public function __construct(RepositoryInterface $repository, array $variables)
  31. {
  32. $this->repository = $repository;
  33. $this->variables = $variables;
  34. }
  35. /**
  36. * Assert that each variable is present.
  37. *
  38. * @throws \Dotenv\Exception\ValidationException
  39. *
  40. * @return \Dotenv\Validator
  41. */
  42. public function required()
  43. {
  44. return $this->assert(
  45. static function (?string $value) {
  46. return $value !== null;
  47. },
  48. 'is missing'
  49. );
  50. }
  51. /**
  52. * Assert that each variable is not empty.
  53. *
  54. * @throws \Dotenv\Exception\ValidationException
  55. *
  56. * @return \Dotenv\Validator
  57. */
  58. public function notEmpty()
  59. {
  60. return $this->assertNullable(
  61. static function (string $value) {
  62. return Str::len(\trim($value)) > 0;
  63. },
  64. 'is empty'
  65. );
  66. }
  67. /**
  68. * Assert that each specified variable is an integer.
  69. *
  70. * @throws \Dotenv\Exception\ValidationException
  71. *
  72. * @return \Dotenv\Validator
  73. */
  74. public function isInteger()
  75. {
  76. return $this->assertNullable(
  77. static function (string $value) {
  78. return \ctype_digit($value);
  79. },
  80. 'is not an integer'
  81. );
  82. }
  83. /**
  84. * Assert that each specified variable is a boolean.
  85. *
  86. * @throws \Dotenv\Exception\ValidationException
  87. *
  88. * @return \Dotenv\Validator
  89. */
  90. public function isBoolean()
  91. {
  92. return $this->assertNullable(
  93. static function (string $value) {
  94. if ($value === '') {
  95. return false;
  96. }
  97. return \filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE) !== null;
  98. },
  99. 'is not a boolean'
  100. );
  101. }
  102. /**
  103. * Assert that each variable is amongst the given choices.
  104. *
  105. * @param string[] $choices
  106. *
  107. * @throws \Dotenv\Exception\ValidationException
  108. *
  109. * @return \Dotenv\Validator
  110. */
  111. public function allowedValues(array $choices)
  112. {
  113. return $this->assertNullable(
  114. static function (string $value) use ($choices) {
  115. return \in_array($value, $choices, true);
  116. },
  117. \sprintf('is not one of [%s]', \implode(', ', $choices))
  118. );
  119. }
  120. /**
  121. * Assert that each variable matches the given regular expression.
  122. *
  123. * @param string $regex
  124. *
  125. * @throws \Dotenv\Exception\ValidationException
  126. *
  127. * @return \Dotenv\Validator
  128. */
  129. public function allowedRegexValues(string $regex)
  130. {
  131. return $this->assertNullable(
  132. static function (string $value) use ($regex) {
  133. return Regex::matches($regex, $value)->success()->getOrElse(false);
  134. },
  135. \sprintf('does not match "%s"', $regex)
  136. );
  137. }
  138. /**
  139. * Assert that the callback returns true for each variable.
  140. *
  141. * @param callable(?string):bool $callback
  142. * @param string $message
  143. *
  144. * @throws \Dotenv\Exception\ValidationException
  145. *
  146. * @return \Dotenv\Validator
  147. */
  148. public function assert(callable $callback, string $message)
  149. {
  150. $failing = [];
  151. foreach ($this->variables as $variable) {
  152. if ($callback($this->repository->get($variable)) === false) {
  153. $failing[] = \sprintf('%s %s', $variable, $message);
  154. }
  155. }
  156. if (\count($failing) > 0) {
  157. throw new ValidationException(\sprintf(
  158. 'One or more environment variables failed assertions: %s.',
  159. \implode(', ', $failing)
  160. ));
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Assert that the callback returns true for each variable.
  166. *
  167. * Skip checking null variable values.
  168. *
  169. * @param callable(string):bool $callback
  170. * @param string $message
  171. *
  172. * @throws \Dotenv\Exception\ValidationException
  173. *
  174. * @return \Dotenv\Validator
  175. */
  176. public function assertNullable(callable $callback, string $message)
  177. {
  178. return $this->assert(
  179. static function (?string $value) use ($callback) {
  180. if ($value === null) {
  181. return true;
  182. }
  183. return $callback($value);
  184. },
  185. $message
  186. );
  187. }
  188. }