Cml\Console\Component\Dialog::confirm PHP Method

confirm() public method

if($dialog->confirm('Are you sure?')) { ... } if($dialog->confirm('Your choice?', null, ['a', 'b', 'c'])) { ... }
public confirm ( string $question, array $choices = ['Y', 'n'], string $answer = 'y', string $default = 'y', string $errorMessage = 'Invalid choice' ) : boolean
$question string 问题
$choices array 选项
$answer string 通过的答案
$default string 默认选项
$errorMessage string 错误信息
return boolean
    public function confirm($question, array $choices = ['Y', 'n'], $answer = 'y', $default = 'y', $errorMessage = 'Invalid choice')
    {
        $text = $question . ' [' . implode('/', $choices) . ']';
        $choices = array_map('strtolower', $choices);
        $answer = strtolower($answer);
        $default = strtolower($default);
        do {
            $input = strtolower($this->ask($text));
            if (in_array($input, $choices)) {
                return $input === $answer;
            } else {
                if (empty($input) && !empty($default)) {
                    return $default === $answer;
                }
            }
            Output::writeln($errorMessage);
            return false;
        } while (true);
    }

Usage Example

Example #1
0
 /**
  * 创建一个新的seeder
  *
  * @param array $args 参数
  * @param array $options 选项
  */
 public function execute(array $args, array $options = [])
 {
     $this->bootstrap($args, $options);
     // get the seed path from the config
     $path = $this->getConfig()->getSeedPath();
     if (!file_exists($path)) {
         $ask = new Dialog();
         if ($ask->confirm(Colour::colour('Create seeds directory?', [Colour::RED, Colour::HIGHLIGHT]))) {
             mkdir($path, 0755, true);
         }
     }
     $this->verifySeedDirectory($path);
     $path = realpath($path);
     $className = $args[0];
     if (!Util::isValidPhinxClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The seed class name "%s" is invalid. Please use CamelCase format', $className));
     }
     // Compute the file path
     $filePath = $path . DIRECTORY_SEPARATOR . $className . '.php';
     if (is_file($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', basename($filePath)));
     }
     // inject the class names appropriate to this seeder
     $contents = file_get_contents($this->getSeedTemplateFilename());
     $classes = ['$useClassName' => 'Phinx\\Seed\\AbstractSeed', '$className' => $className, '$baseClassName' => 'AbstractSeed'];
     $contents = strtr($contents, $classes);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     Output::writeln('using seed base class ' . $classes['$useClassName']);
     Output::writeln('created ' . str_replace(Cml::getApplicationDir('secure_src'), '{secure_src}', $filePath));
 }
All Usage Examples Of Cml\Console\Component\Dialog::confirm