Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
YesNo.php
Go to the documentation of this file.
1 <?php
8 
11 use Symfony\Component\Console\Helper\QuestionHelper;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Question\Question;
15 use Symfony\Component\Console\Question\QuestionFactory;
16 
20 class YesNo
21 {
27  private $questionHelper;
28 
34  private $questionFactory;
35 
40  public function __construct(
41  QuestionHelper $questionHelper,
42  QuestionFactory $questionFactory
43  ) {
44  $this->questionHelper = $questionHelper;
45  $this->questionFactory = $questionFactory;
46  }
47 
56  public function execute(array $messages, InputInterface $input, OutputInterface $output)
57  {
58  if (!$input->isInteractive()) {
59  return true;
60  }
61 
62  $question = $this->getConfirmationQuestion($messages);
63  $answer = $this->questionHelper->ask($input, $output, $question);
64 
65  return in_array(strtolower($answer), ['yes', 'y']);
66  }
67 
75  private function getConfirmationQuestion(array $messages)
76  {
78  $question = $this->questionFactory->create([
79  'question' => implode(PHP_EOL, $messages) . PHP_EOL
80  ]);
81 
82  $question->setValidator(function ($answer) {
83  if (!in_array(strtolower($answer), ['yes', 'y', 'no', 'n'])) {
84  throw new LocalizedException(
85  new Phrase('A [y]es or [n]o selection needs to be made. Select and try again.')
86  );
87  }
88 
89  return $answer;
90  });
91 
92  return $question;
93  }
94 }
execute(array $messages, InputInterface $input, OutputInterface $output)
Definition: YesNo.php:56
__construct(QuestionHelper $questionHelper, QuestionFactory $questionFactory)
Definition: YesNo.php:40