ConfigValidation.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\config_test;
  3. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  4. /**
  5. * Provides a collection of validation callbacks for testing purposes.
  6. */
  7. class ConfigValidation {
  8. /**
  9. * Validates a llama.
  10. *
  11. * @param string $string
  12. * The string to validate.
  13. * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
  14. * The validation execution context.
  15. */
  16. public static function validateLlama($string, ExecutionContextInterface $context) {
  17. if (!in_array($string, ['llama', 'alpaca', 'guanaco', 'vicuña'], TRUE)) {
  18. $context->addViolation('no valid llama');
  19. }
  20. }
  21. /**
  22. * Validates cats.
  23. *
  24. * @param string $string
  25. * The string to validate.
  26. * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
  27. * The validation execution context.
  28. */
  29. public static function validateCats($string, ExecutionContextInterface $context) {
  30. if (!in_array($string, ['kitten', 'cats', 'nyans'])) {
  31. $context->addViolation('no valid cat');
  32. }
  33. }
  34. /**
  35. * Validates a number.
  36. *
  37. * @param int $count
  38. * The integer to validate.
  39. * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
  40. * The validation execution context.
  41. */
  42. public static function validateCatCount($count, ExecutionContextInterface $context) {
  43. if ($count <= 1) {
  44. $context->addViolation('no enough cats');
  45. }
  46. }
  47. /**
  48. * Validates giraffes.
  49. *
  50. * @param string $string
  51. * The string to validate.
  52. * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
  53. * The validation execution context.
  54. */
  55. public static function validateGiraffes($string, ExecutionContextInterface $context) {
  56. if (strpos($string, 'hum') !== 0) {
  57. $context->addViolation('Giraffes just hum');
  58. }
  59. }
  60. /**
  61. * Validates a mapping.
  62. *
  63. * @param array $mapping
  64. * The data to validate.
  65. * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
  66. * The validation execution context.
  67. */
  68. public static function validateMapping($mapping, ExecutionContextInterface $context) {
  69. if ($diff = array_diff(array_keys($mapping), ['llama', 'cat', 'giraffe', 'uuid', '_core'])) {
  70. $context->addViolation('Missing giraffe.');
  71. }
  72. }
  73. /**
  74. * Validates a sequence.
  75. *
  76. * @param array $sequence
  77. * The data to validate.
  78. * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
  79. * The validation execution context.
  80. */
  81. public static function validateSequence($sequence, ExecutionContextInterface $context) {
  82. if (isset($sequence['invalid-key'])) {
  83. $context->addViolation('Invalid giraffe key.');
  84. }
  85. }
  86. }