Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Console / Prompt / Number.php
blob7bb299bb728be3e042871144fedf414fc2189e93
1 <?php
2 /**
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
8 */
10 namespace Zend\Console\Prompt;
12 class Number extends Line
14 /**
15 * @var string
17 protected $promptText = 'Please enter a number: ';
19 /**
20 * @var bool
22 protected $allowFloat = false;
24 /**
25 * @var int
27 protected $min;
29 /**
30 * @var int
32 protected $max;
34 /**
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: ',
45 $allowEmpty = false,
46 $allowFloat = false,
47 $min = null,
48 $max = null
49 ) {
50 if ($promptText !== null) {
51 $this->setPromptText($promptText);
54 if ($allowEmpty !== null) {
55 $this->setAllowEmpty($allowEmpty);
58 if ($min !== null) {
59 $this->setMin($min);
62 if ($max !== null) {
63 $this->setMax($max);
66 if ($allowFloat !== null) {
67 $this->setAllowFloat($allowFloat);
71 /**
72 * Show the prompt to user and return the answer.
74 * @return mixed
76 public function show()
78 /**
79 * Ask for a number and validate it.
81 do {
82 $valid = true;
83 $number = parent::show();
84 if ($number === "" && !$this->allowEmpty) {
85 $valid = false;
86 } elseif ($number === "") {
87 $number = null;
88 } elseif (!is_numeric($number)) {
89 $this->getConsole()->writeLine("$number is not a number\n");
90 $valid = false;
91 } elseif (!$this->allowFloat && (round($number) != $number)) {
92 $this->getConsole()->writeLine("Please enter a non-floating number, i.e. " . round($number) . "\n");
93 $valid = false;
94 } elseif ($this->max !== null && $number > $this->max) {
95 $this->getConsole()->writeLine("Please enter a number not greater than " . $this->max . "\n");
96 $valid = false;
97 } elseif ($this->min !== null && $number < $this->min) {
98 $this->getConsole()->writeLine("Please enter a number not smaller than " . $this->min . "\n");
99 $valid = false;
101 } while (!$valid);
104 * Cast proper type
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;
122 * @return bool
124 public function getAllowEmpty()
126 return $this->allowEmpty;
130 * @param int $maxLength
132 public function setMaxLength($maxLength)
134 $this->maxLength = $maxLength;
138 * @return int
140 public function getMaxLength()
142 return $this->maxLength;
146 * @param string $promptText
148 public function setPromptText($promptText)
150 $this->promptText = $promptText;
154 * @return string
156 public function getPromptText()
158 return $this->promptText;
162 * @param int $max
164 public function setMax($max)
166 $this->max = $max;
170 * @return int
172 public function getMax()
174 return $this->max;
178 * @param int $min
180 public function setMin($min)
182 $this->min = $min;
186 * @return int
188 public function getMin()
190 return $this->min;
194 * @param bool $allowFloat
196 public function setAllowFloat($allowFloat)
198 $this->allowFloat = $allowFloat;
202 * @return bool
204 public function getAllowFloat()
206 return $this->allowFloat;