Guest User

Untitled

a guest
Jan 17th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SyzerPHPEnvironment;
  4. /**
  5. * Config.
  6. */
  7. class Config extends Environment
  8. {
  9. /**
  10. * validate().
  11. *
  12. * Validate the config array.
  13. *
  14. * @param array $conf The config array.
  15. *
  16. * @throws DomainException If `$conf` argument is empty.
  17. * @throws DomainException If `$conf` argument does not have a depth of 2.
  18. * @throws UnexpectedValueException If a section is not the start of an array.
  19. * @throws UnexpectedValueException If the section variable names are not capital letters.
  20. * @throws LengthException If the section variable name and/or value is too long.
  21. *
  22. * @return void.
  23. */
  24. public static function validate(array $conf): void
  25. {
  26. if (empty($conf)) {
  27. throw new ExceptionDomainException('The config array is empty.');
  28. }
  29. if (depth($conf) != 2) {
  30. throw new ExceptionDomainException(sprintf(
  31. 'The config array does not have a depth of 2. Depth: `%s`.',
  32. (string) depth($conf)
  33. ));
  34. }
  35. foreach ($conf as $var => $val) {
  36. if (!is_array($val)) {
  37. throw new ExceptionUnexpectedValueException(sprintf(
  38. 'The section is not the start of an array. Passed: `%s`.',
  39. e($var)
  40. ));
  41. }
  42. foreach ($val as $var2 => $val2) {
  43. if (!ctype_upper(str_replace('_', '', $var2))) {
  44. throw new ExceptionUnexpectedValueException(sprintf(
  45. 'The section variable name must all be caps. Passed: `%s`.',
  46. e($var2)
  47. ));
  48. }
  49. if (strlen($var2) > 30 || strlen($val2) > 250) {
  50. throw new ExceptionLengthException(sprintf(
  51. 'The `$var2` and/or `$val2` variable is too long. Passed: `$var2` = `%s` `$val2` = `%s`.',
  52. (string) strlen($var2),
  53. (string) strlen($val2)
  54. ));
  55. }
  56. }
  57. }
  58. self::clearConfig();
  59. self::$conf = $conf;
  60. }
  61. }
Add Comment
Please, Sign In to add comment