Number::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace PhpSchool\CliMenu\Input;
4
5
use PhpSchool\CliMenu\MenuStyle;
6
use PhpSchool\Terminal\InputCharacter;
7
8
/**
9
 * @author Aydin Hassan <[email protected]>
10
 */
11
class Number implements Input
12
{
13
    /**
14
     * @var InputIO
15
     */
16
    private $inputIO;
17
18
    /**
19
     * @var string
20
     */
21
    private $promptText = 'Enter a number:';
22
23
    /**
24
     * @var string
25
     */
26
    private $validationFailedText = 'Not a valid number, try again';
27
28
    /**
29
     * @var string
30
     */
31
    private $placeholderText = '';
32
33
    /**
34
     * @var null|callable
35
     */
36
    private $validator;
37
38
    /**
39
     * @var MenuStyle
40
     */
41
    private $style;
42
43
    public function __construct(InputIO $inputIO, MenuStyle $style)
44
    {
45
        $this->inputIO = $inputIO;
46
        $this->style = $style;
47
    }
48
49
    public function setPromptText(string $promptText) : Input
50
    {
51
        $this->promptText = $promptText;
52
53
        return $this;
54
    }
55
56
    public function getPromptText() : string
57
    {
58
        return $this->promptText;
59
    }
60
61
    public function setValidationFailedText(string $validationFailedText) : Input
62
    {
63
        $this->validationFailedText = $validationFailedText;
64
65
        return $this;
66
    }
67
68
    public function getValidationFailedText() : string
69
    {
70
        return $this->validationFailedText;
71
    }
72
73
    public function setPlaceholderText(string $placeholderText) : Input
74
    {
75
        $this->placeholderText = $placeholderText;
76
77
        return $this;
78
    }
79
80
    public function getPlaceholderText() : string
81
    {
82
        return $this->placeholderText;
83
    }
84
85
    public function setValidator(callable $validator) : Input
86
    {
87
        $this->validator = $validator;
88
        
89
        return $this;
90
    }
91
92
    public function ask() : InputResult
93
    {
94
        $this->inputIO->registerControlCallback(InputCharacter::UP, function (string $input) {
95
            return $this->validate($input) ? (int) $input + 1 : $input;
96
        });
97
98
        $this->inputIO->registerControlCallback(InputCharacter::DOWN, function (string $input) {
99
            return $this->validate($input) ? (int) $input - 1 : $input;
100
        });
101
102
        return $this->inputIO->collect($this);
103
    }
104
105
    public function validate(string $input) : bool
106
    {
107
        if ($this->validator) {
108
            $validator = $this->validator;
109
            
110
            if ($validator instanceof \Closure) {
111
                $validator = $validator->bindTo($this);
112
            }
113
            
114
            return $validator($input);
115
        }
116
117
        return (bool) preg_match('/^-?\d+$/', $input);
118
    }
119
120
    public function filter(string $value) : string
121
    {
122
        return $value;
123
    }
124
125
    public function getStyle() : MenuStyle
126
    {
127
        return $this->style;
128
    }
129
}
130