Нет описания

Validator.php 4.6KB

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