1<?php
2/**
3 * File containing the ezcConsoleDialogViewer class.
4 *
5 * @package ConsoleTools
6 * @version 1.6.1
7 * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
9 * @filesource
10 */
11
12/**
13 * Utility class for ezcConsoleDialog implementations.
14 * This class contains utility methods for working with {@link
15 * ezcConsoleDialog} implementations.
16 *
17 * To display a dialog in a loop until a valid result is received do:
18 * <code>
19 * // Instatiate dialog in $dialog ...
20 * ezcConsoleDialogViewer::displayDialog( $dialog );
21 * </code>
22 *
23 * For implementing a custom dialog, the method {@link readLine()} method can be
24 * used to read a line of input from the user.
25 *
26 * @package ConsoleTools
27 * @version 1.6.1
28 */
29class ezcConsoleDialogViewer
30{
31    /**
32     * Displays a dialog and returns a valid result from it.
33     * This methods displays a dialog in a loop, until it received a valid
34     * result from it and returns this result.
35     *
36     * @param ezcConsoleDialog $dialog The dialog to display.
37     * @return mixed The result from this dialog.
38     */
39    public static function displayDialog( ezcConsoleDialog $dialog )
40    {
41        do
42        {
43            $dialog->display();
44        }
45        while ( $dialog->hasValidResult() === false );
46        return $dialog->getResult();
47    }
48
49    /**
50     * Returns a line from STDIN.
51     * The returned line is fully trimmed.
52     *
53     * @return string
54     * @throws ezcConsoleDialogAbortException
55     *         if the user closes STDIN using <CTRL>-D.
56     */
57    public static function readLine()
58    {
59        $res = trim( fgets( STDIN ) );
60        if ( feof( STDIN ) )
61        {
62            throw new ezcConsoleDialogAbortException();
63        }
64        return $res;
65    }
66}
67
68?>
69