3 namespace DrupalCodeGenerator\Command\Drupal_8\Plugin;
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;
13 * Implements d8:plugin:constraint command.
15 class Constraint extends BaseGenerator {
17 protected $name = 'd8:plugin:constraint';
18 protected $description = 'Generates constraint plugin';
19 protected $alias = 'constraint';
24 protected function interact(InputInterface $input, OutputInterface $output) {
25 $questions = Utils::defaultPluginQuestions();
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']);
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.');
38 $questions['plugin_id']->setValidator($plugin_id_validator);
42 'item_list' => 'Item list',
44 'raw_value' => 'Raw value',
46 $type_choices = Utils::prepareChoices($input_types);
47 $questions['input_type'] = new ChoiceQuestion('Type of data to validate', $type_choices, 'Item list');
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);
54 ->path('src/Plugin/Validation/Constraint/{class}.php')
55 ->template('d8/plugin/constraint.twig');
58 ->path('src/Plugin/Validation/Constraint/{class}Validator.php')
59 ->template('d8/plugin/constraint-validator.twig');