Просмотр файла vendor/vlucas/phpdotenv/src/Validator.php

Размер файла: 3.1Kb
  1. <?php
  2.  
  3. namespace Dotenv;
  4.  
  5. use Dotenv\Exception\InvalidCallbackException;
  6. use Dotenv\Exception\ValidationException;
  7.  
  8. /**
  9. * This is the validator class.
  10. *
  11. * It's responsible for applying validations against a number of variables.
  12. */
  13. class Validator
  14. {
  15. /**
  16. * The variables to validate.
  17. *
  18. * @var array
  19. */
  20. protected $variables;
  21.  
  22. /**
  23. * The loader instance.
  24. *
  25. * @var \Dotenv\Loader
  26. */
  27. protected $loader;
  28.  
  29. /**
  30. * Create a new validator instance.
  31. *
  32. * @param array $variables
  33. * @param \Dotenv\Loader $loader
  34. *
  35. * @return void
  36. */
  37. public function __construct(array $variables, Loader $loader)
  38. {
  39. $this->variables = $variables;
  40. $this->loader = $loader;
  41.  
  42. $this->assertCallback(
  43. function ($value) {
  44. return $value !== null;
  45. },
  46. 'is missing'
  47. );
  48. }
  49.  
  50. /**
  51. * Assert that each variable is not empty.
  52. *
  53. * @return \Dotenv\Validator
  54. */
  55. public function notEmpty()
  56. {
  57. return $this->assertCallback(
  58. function ($value) {
  59. return strlen(trim($value)) > 0;
  60. },
  61. 'is empty'
  62. );
  63. }
  64.  
  65. /**
  66. * Assert that each specified variable is an integer.
  67. *
  68. * @return \Dotenv\Validator
  69. */
  70. public function isInteger()
  71. {
  72. return $this->assertCallback(
  73. function ($value) {
  74. return ctype_digit($value);
  75. },
  76. 'is not an integer'
  77. );
  78. }
  79.  
  80. /**
  81. * Assert that each variable is amongst the given choices.
  82. *
  83. * @param string[] $choices
  84. *
  85. * @return \Dotenv\Validator
  86. */
  87. public function allowedValues(array $choices)
  88. {
  89. return $this->assertCallback(
  90. function ($value) use ($choices) {
  91. return in_array($value, $choices);
  92. },
  93. 'is not an allowed value'
  94. );
  95. }
  96.  
  97. /**
  98. * Assert that the callback returns true for each variable.
  99. *
  100. * @param callable $callback
  101. * @param string $message
  102. *
  103. * @throws \Dotenv\Exception\InvalidCallbackException|\Dotenv\Exception\ValidationException
  104. *
  105. * @return \Dotenv\Validator
  106. */
  107. protected function assertCallback($callback, $message = 'failed callback assertion')
  108. {
  109. if (!is_callable($callback)) {
  110. throw new InvalidCallbackException('The provided callback must be callable.');
  111. }
  112.  
  113. $variablesFailingAssertion = array();
  114. foreach ($this->variables as $variableName) {
  115. $variableValue = $this->loader->getEnvironmentVariable($variableName);
  116. if (call_user_func($callback, $variableValue) === false) {
  117. $variablesFailingAssertion[] = $variableName." $message";
  118. }
  119. }
  120.  
  121. if (count($variablesFailingAssertion) > 0) {
  122. throw new ValidationException(sprintf(
  123. 'One or more environment variables failed assertions: %s.',
  124. implode(', ', $variablesFailingAssertion)
  125. ));
  126. }
  127.  
  128. return $this;
  129. }
  130. }