Validator.php 5.1 KB

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