Completed
Push — master ( 627059...4c883f )
by Michael
9s
created

Number   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 97
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPromptText() 0 6 1
A getPromptText() 0 4 1
A setValidationFailedText() 0 6 1
A getValidationFailedText() 0 4 1
A setPlaceholderText() 0 6 1
A getPlaceholderText() 0 4 1
A ask() 0 12 3
A validate() 0 4 1
A filter() 0 4 1
A getStyle() 0 4 1
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 MenuStyle
35
     */
36
    private $style;
37
38
    public function __construct(InputIO $inputIO, MenuStyle $style)
39
    {
40
        $this->inputIO = $inputIO;
41
        $this->style = $style;
42
    }
43
44
    public function setPromptText(string $promptText) : Input
45
    {
46
        $this->promptText = $promptText;
47
48
        return $this;
49
    }
50
51
    public function getPromptText() : string
52
    {
53
        return $this->promptText;
54
    }
55
56
    public function setValidationFailedText(string $validationFailedText) : Input
57
    {
58
        $this->validationFailedText = $validationFailedText;
59
60
        return $this;
61
    }
62
63
    public function getValidationFailedText() : string
64
    {
65
        return $this->validationFailedText;
66
    }
67
68
    public function setPlaceholderText(string $placeholderText) : Input
69
    {
70
        $this->placeholderText = $placeholderText;
71
72
        return $this;
73
    }
74
75
    public function getPlaceholderText() : string
76
    {
77
        return $this->placeholderText;
78
    }
79
80
    public function ask() : InputResult
81
    {
82
        $this->inputIO->registerControlCallback(InputCharacter::UP, function (string $input) {
83
            return $this->validate($input) ? $input + 1 : $input;
84
        });
85
86
        $this->inputIO->registerControlCallback(InputCharacter::DOWN, function (string $input) {
87
            return $this->validate($input) ? $input - 1 : $input;
88
        });
89
90
        return $this->inputIO->collect($this);
91
    }
92
93
    public function validate(string $input) : bool
94
    {
95
        return (bool) preg_match('/^-?\d+$/', $input);
96
    }
97
98
    public function filter(string $value) : string
99
    {
100
        return $value;
101
    }
102
103
    public function getStyle() : MenuStyle
104
    {
105
        return $this->style;
106
    }
107
}
108