Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Number.php « Prompt « src « zend-console « zendframework « vendor - github.com/bareos/bareos-webui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f096a7e13d65224688705f3603437aca8f01312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Console\Prompt;

class Number extends Line
{
    /**
     * @var string
     */
    protected $promptText = 'Please enter a number: ';

    /**
     * @var bool
     */
    protected $allowFloat = false;

    /**
     * @var int
     */
    protected $min;

    /**
     * @var int
     */
    protected $max;

    /**
     * Ask the user for a number.
     *
     * @param string    $promptText     The prompt text to display in console
     * @param bool      $allowEmpty     Is empty response allowed?
     * @param bool      $allowFloat     Are floating (non-decimal) numbers allowed?
     * @param int   $min            Minimum value (inclusive)
     * @param int   $max            Maximum value (inclusive)
     */
    public function __construct(
        $promptText = 'Please enter a number: ',
        $allowEmpty = false,
        $allowFloat = false,
        $min = null,
        $max = null
    ) {
        if ($promptText !== null) {
            $this->setPromptText($promptText);
        }

        if ($allowEmpty !== null) {
            $this->setAllowEmpty($allowEmpty);
        }

        if ($min !== null) {
            $this->setMin($min);
        }

        if ($max !== null) {
            $this->setMax($max);
        }

        if ($allowFloat !== null) {
            $this->setAllowFloat($allowFloat);
        }
    }

    /**
     * Show the prompt to user and return the answer.
     *
     * @return mixed
     */
    public function show()
    {
        /**
         * Ask for a number and validate it.
         */
        do {
            $valid = true;
            $number = parent::show();
            if ($number === "" && !$this->allowEmpty) {
                $valid = false;
            } elseif ($number === "") {
                $number = null;
            } elseif (!is_numeric($number)) {
                $this->getConsole()->writeLine("$number is not a number\n");
                $valid = false;
            } elseif (!$this->allowFloat && (round($number) != $number)) {
                $this->getConsole()->writeLine("Please enter a non-floating number, i.e. " . round($number) . "\n");
                $valid = false;
            } elseif ($this->max !== null && $number > $this->max) {
                $this->getConsole()->writeLine("Please enter a number not greater than " . $this->max . "\n");
                $valid = false;
            } elseif ($this->min !== null && $number < $this->min) {
                $this->getConsole()->writeLine("Please enter a number not smaller than " . $this->min . "\n");
                $valid = false;
            }
        } while (!$valid);

        /**
         * Cast proper type
         */
        if ($number !== null) {
            $number = $this->allowFloat ? (double) $number : (int) $number;
        }

        return $this->lastResponse = $number;
    }

    /**
     * @param  bool $allowEmpty
     */
    public function setAllowEmpty($allowEmpty)
    {
        $this->allowEmpty = $allowEmpty;
    }

    /**
     * @return bool
     */
    public function getAllowEmpty()
    {
        return $this->allowEmpty;
    }

    /**
     * @param int $maxLength
     */
    public function setMaxLength($maxLength)
    {
        $this->maxLength = $maxLength;
    }

    /**
     * @return int
     */
    public function getMaxLength()
    {
        return $this->maxLength;
    }

    /**
     * @param string $promptText
     */
    public function setPromptText($promptText)
    {
        $this->promptText = $promptText;
    }

    /**
     * @return string
     */
    public function getPromptText()
    {
        return $this->promptText;
    }

    /**
     * @param int $max
     */
    public function setMax($max)
    {
        $this->max = $max;
    }

    /**
     * @return int
     */
    public function getMax()
    {
        return $this->max;
    }

    /**
     * @param int $min
     */
    public function setMin($min)
    {
        $this->min = $min;
    }

    /**
     * @return int
     */
    public function getMin()
    {
        return $this->min;
    }

    /**
     * @param  bool $allowFloat
     */
    public function setAllowFloat($allowFloat)
    {
        $this->allowFloat = $allowFloat;
    }

    /**
     * @return bool
     */
    public function getAllowFloat()
    {
        return $this->allowFloat;
    }
}