4a1fd1dda65fb07088762898b5b83100b2c80a04
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / Plugin / Constraint.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_8\Plugin;
4
5 use DrupalCodeGenerator\Command\BaseGenerator;
6 use DrupalCodeGenerator\Utils;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Question\ChoiceQuestion;
10 use Symfony\Component\Console\Question\Question;
11
12 /**
13  * Implements d8:plugin:constraint command.
14  */
15 class Constraint extends BaseGenerator {
16
17   protected $name = 'd8:plugin:constraint';
18   protected $description = 'Generates constraint plugin';
19   protected $alias = 'constraint';
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function interact(InputInterface $input, OutputInterface $output) {
25     $questions = Utils::defaultPluginQuestions();
26
27     $default_plugin_id = function (array $vars) {
28       // Unlike other plugin types. Constraint IDs use camel case.
29       return Utils::camelize($vars['name'] . $vars['plugin_label']);
30     };
31     $questions['plugin_id'] = new Question('Constraint ID', $default_plugin_id);
32     $plugin_id_validator = function ($value) {
33       if (!preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/i', $value)) {
34         throw new \UnexpectedValueException('The value is not correct machine name.');
35       }
36       return $value;
37     };
38     $questions['plugin_id']->setValidator($plugin_id_validator);
39
40     $input_types = [
41       'entity' => 'Entity',
42       'item_list' => 'Item list',
43       'item' => 'Item',
44       'raw_value' => 'Raw value',
45     ];
46     $type_choices = Utils::prepareChoices($input_types);
47     $questions['input_type'] = new ChoiceQuestion('Type of data to validate', $type_choices, 'Item list');
48
49     $vars = &$this->collectVars($input, $output, $questions);
50     $vars['class'] = Utils::camelize($vars['plugin_label']) . 'Constraint';
51     $vars['input_type'] = array_search($vars['input_type'], $input_types);
52
53     $this->addFile()
54       ->path('src/Plugin/Validation/Constraint/{class}.php')
55       ->template('d8/plugin/constraint.twig');
56
57     $this->addFile()
58       ->path('src/Plugin/Validation/Constraint/{class}Validator.php')
59       ->template('d8/plugin/constraint-validator.twig');
60   }
61
62 }