source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/Symfony/Component/Console/Helper/DialogHelper.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 3.4 KB
Line 
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Console\Helper;
13
14use Symfony\Component\Console\Output\OutputInterface;
15
16/**
17 * The Dialog class provides helpers to interact with the user.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 */
21class DialogHelper extends Helper
22{
23    /**
24     * Asks a question to the user.
25     *
26     * @param OutputInterface $output
27     * @param string|array    $question The question to ask
28     * @param string          $default  The default answer if none is given by the user
29     *
30     * @return string The user answer
31     */
32    public function ask(OutputInterface $output, $question, $default = null)
33    {
34        // @codeCoverageIgnoreStart
35        $output->write($question);
36
37        $ret = trim(fgets(STDIN));
38
39        return $ret ? $ret : $default;
40        // @codeCoverageIgnoreEnd
41    }
42
43    /**
44     * Asks a confirmation to the user.
45     *
46     * The question will be asked until the user answer by nothing, yes, or no.
47     *
48     * @param OutputInterface $output
49     * @param string|array    $question The question to ask
50     * @param Boolean         $default  The default answer if the user enters nothing
51     *
52     * @return Boolean true if the user has confirmed, false otherwise
53     */
54    public function askConfirmation(OutputInterface $output, $question, $default = true)
55    {
56        // @codeCoverageIgnoreStart
57        $answer = 'z';
58        while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
59            $answer = $this->ask($output, $question);
60        }
61
62        if (false === $default) {
63            return $answer && 'y' == strtolower($answer[0]);
64        }
65
66        return !$answer || 'y' == strtolower($answer[0]);
67        // @codeCoverageIgnoreEnd
68    }
69
70    /**
71     * Asks for a value and validates the response.
72     *
73     * The validator receives the data to validate. It must return the
74     * validated data when the data is valid and throw an exception
75     * otherwise.
76     *
77     * @param OutputInterface $output
78     * @param string|array    $question
79     * @param callback        $validator A PHP callback
80     * @param integer         $attempts Max number of times to ask before giving up (false by default, which means infinite)
81     * @param string          $default  The default answer if none is given by the user
82     *
83     * @return mixed
84     *
85     * @throws \Exception When any of the validator returns an error
86     */
87    public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
88    {
89        // @codeCoverageIgnoreStart
90        $error = null;
91        while (false === $attempts || $attempts--) {
92            if (null !== $error) {
93                $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
94            }
95
96            $value = $this->ask($output, $question, $default);
97
98            try {
99                return call_user_func($validator, $value);
100            } catch (\Exception $error) {
101            }
102        }
103
104        throw $error;
105        // @codeCoverageIgnoreEnd
106    }
107
108    /**
109     * Returns the helper's canonical name
110     */
111    public function getName()
112    {
113        return 'dialog';
114    }
115}
Note: See TracBrowser for help on using the repository browser.