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