3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Console\Prompt
;
12 class Number
extends Line
17 protected $promptText = 'Please enter a number: ';
22 protected $allowFloat = false;
35 * Ask the user for a number.
37 * @param string $promptText The prompt text to display in console
38 * @param bool $allowEmpty Is empty response allowed?
39 * @param bool $allowFloat Are floating (non-decimal) numbers allowed?
40 * @param int $min Minimum value (inclusive)
41 * @param int $max Maximum value (inclusive)
43 public function __construct(
44 $promptText = 'Please enter a number: ',
50 if ($promptText !== null) {
51 $this->setPromptText($promptText);
54 if ($allowEmpty !== null) {
55 $this->setAllowEmpty($allowEmpty);
66 if ($allowFloat !== null) {
67 $this->setAllowFloat($allowFloat);
72 * Show the prompt to user and return the answer.
76 public function show()
79 * Ask for a number and validate it.
83 $number = parent
::show();
84 if ($number === "" && !$this->allowEmpty
) {
86 } elseif ($number === "") {
88 } elseif (!is_numeric($number)) {
89 $this->getConsole()->writeLine("$number is not a number\n");
91 } elseif (!$this->allowFloat
&& (round($number) != $number)) {
92 $this->getConsole()->writeLine("Please enter a non-floating number, i.e. " . round($number) . "\n");
94 } elseif ($this->max
!== null && $number > $this->max
) {
95 $this->getConsole()->writeLine("Please enter a number not greater than " . $this->max
. "\n");
97 } elseif ($this->min
!== null && $number < $this->min
) {
98 $this->getConsole()->writeLine("Please enter a number not smaller than " . $this->min
. "\n");
106 if ($number !== null) {
107 $number = $this->allowFloat ?
(double) $number : (int) $number;
110 return $this->lastResponse
= $number;
114 * @param bool $allowEmpty
116 public function setAllowEmpty($allowEmpty)
118 $this->allowEmpty
= $allowEmpty;
124 public function getAllowEmpty()
126 return $this->allowEmpty
;
130 * @param int $maxLength
132 public function setMaxLength($maxLength)
134 $this->maxLength
= $maxLength;
140 public function getMaxLength()
142 return $this->maxLength
;
146 * @param string $promptText
148 public function setPromptText($promptText)
150 $this->promptText
= $promptText;
156 public function getPromptText()
158 return $this->promptText
;
164 public function setMax($max)
172 public function getMax()
180 public function setMin($min)
188 public function getMin()
194 * @param bool $allowFloat
196 public function setAllowFloat($allowFloat)
198 $this->allowFloat
= $allowFloat;
204 public function getAllowFloat()
206 return $this->allowFloat
;