No Description

ConfirmationQuestion.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /**
  19. * Constructor.
  20. *
  21. * @param string $question The question to ask to the user
  22. * @param bool $default The default answer to return, true or false
  23. */
  24. public function __construct($question, $default = true)
  25. {
  26. parent::__construct($question, (bool) $default);
  27. $this->setNormalizer($this->getDefaultNormalizer());
  28. }
  29. /**
  30. * Returns the default answer normalizer.
  31. *
  32. * @return callable
  33. */
  34. private function getDefaultNormalizer()
  35. {
  36. $default = $this->getDefault();
  37. return function ($answer) use ($default) {
  38. if (is_bool($answer)) {
  39. return $answer;
  40. }
  41. if (false === $default) {
  42. return $answer && 'y' === strtolower($answer[0]);
  43. }
  44. return !$answer || 'y' === strtolower($answer[0]);
  45. };
  46. }
  47. }