1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Console\Prompt;
11
12class Line extends AbstractPrompt
13{
14    /**
15     * @var string
16     */
17    protected $promptText = 'Please enter value: ';
18
19    /**
20     * @var bool
21     */
22    protected $allowEmpty = false;
23
24    /**
25     * @var int
26     */
27    protected $maxLength = 2048;
28
29    /**
30     * Ask the user for an answer (a line of text)
31     *
32     * @param string    $promptText     The prompt text to display in console
33     * @param bool      $allowEmpty     Is empty response allowed?
34     * @param int       $maxLength      Maximum response length
35     */
36    public function __construct($promptText = 'Please enter value: ', $allowEmpty = false, $maxLength = 2048)
37    {
38        if ($promptText !== null) {
39            $this->setPromptText($promptText);
40        }
41
42        if ($allowEmpty !== null) {
43            $this->setAllowEmpty($allowEmpty);
44        }
45
46        if ($maxLength !== null) {
47            $this->setMaxLength($maxLength);
48        }
49    }
50
51    /**
52     * Show the prompt to user and return the answer.
53     *
54     * @return string
55     */
56    public function show()
57    {
58        do {
59            $this->getConsole()->write($this->promptText);
60            $line = $this->getConsole()->readLine($this->maxLength);
61        } while (!$this->allowEmpty && !$line);
62
63        return $this->lastResponse = $line;
64    }
65
66    /**
67     * @param  bool $allowEmpty
68     */
69    public function setAllowEmpty($allowEmpty)
70    {
71        $this->allowEmpty = $allowEmpty;
72    }
73
74    /**
75     * @return bool
76     */
77    public function getAllowEmpty()
78    {
79        return $this->allowEmpty;
80    }
81
82    /**
83     * @param int $maxLength
84     */
85    public function setMaxLength($maxLength)
86    {
87        $this->maxLength = $maxLength;
88    }
89
90    /**
91     * @return int
92     */
93    public function getMaxLength()
94    {
95        return $this->maxLength;
96    }
97
98    /**
99     * @param string $promptText
100     */
101    public function setPromptText($promptText)
102    {
103        $this->promptText = $promptText;
104    }
105
106    /**
107     * @return string
108     */
109    public function getPromptText()
110    {
111        return $this->promptText;
112    }
113}
114