Drupal investigation

ConfirmationQuestion.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Question;
  11. /**
  12. * Represents a yes/no question.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class ConfirmationQuestion extends Question
  17. {
  18. private $trueAnswerRegex;
  19. /**
  20. * Constructor.
  21. *
  22. * @param string $question The question to ask to the user
  23. * @param bool $default The default answer to return, true or false
  24. * @param string $trueAnswerRegex A regex to match the "yes" answer
  25. */
  26. public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
  27. {
  28. parent::__construct($question, (bool) $default);
  29. $this->trueAnswerRegex = $trueAnswerRegex;
  30. $this->setNormalizer($this->getDefaultNormalizer());
  31. }
  32. /**
  33. * Returns the default answer normalizer.
  34. *
  35. * @return callable
  36. */
  37. private function getDefaultNormalizer()
  38. {
  39. $default = $this->getDefault();
  40. $regex = $this->trueAnswerRegex;
  41. return function ($answer) use ($default, $regex) {
  42. if (is_bool($answer)) {
  43. return $answer;
  44. }
  45. $answerIsTrue = (bool) preg_match($regex, $answer);
  46. if (false === $default) {
  47. return $answer && $answerIsTrue;
  48. }
  49. return !$answer || $answerIsTrue;
  50. };
  51. }
  52. }