Completed
Push — master ( 83fbce...40f0c1 )
by Aydin
19s queued 11s
created

Text   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidationFailedText() 0 3 1
A setValidator() 0 5 1
A __construct() 0 4 1
A setPlaceholderText() 0 5 1
A getStyle() 0 3 1
A getPlaceholderText() 0 3 1
A getPromptText() 0 3 1
A setPromptText() 0 5 1
A filter() 0 3 1
A ask() 0 3 1
A setValidationFailedText() 0 5 1
A validate() 0 13 3
1
<?php
2
3
namespace PhpSchool\CliMenu\Input;
4
5
use PhpSchool\CliMenu\MenuStyle;
6
7
/**
8
 * @author Aydin Hassan <[email protected]>
9
 */
10
class Text implements Input
11
{
12
    /**
13
     * @var InputIO
14
     */
15
    private $inputIO;
16
17
    /**
18
     * @var string
19
     */
20
    private $promptText = 'Enter text:';
21
22
    /**
23
     * @var string
24
     */
25
    private $validationFailedText = 'Invalid, try again';
26
27
    /**
28
     * @var string
29
     */
30
    private $placeholderText = '';
31
32
    /**
33
     * @var null|callable
34
     */
35
    private $validator;
36
37
    /**
38
     * @var MenuStyle
39
     */
40
    private $style;
41
42
    public function __construct(InputIO $inputIO, MenuStyle $style)
43
    {
44
        $this->inputIO = $inputIO;
45
        $this->style = $style;
46
    }
47
48
    public function setPromptText(string $promptText) : Input
49
    {
50
        $this->promptText = $promptText;
51
52
        return $this;
53
    }
54
55
    public function getPromptText() : string
56
    {
57
        return $this->promptText;
58
    }
59
60
    public function setValidationFailedText(string $validationFailedText) : Input
61
    {
62
        $this->validationFailedText = $validationFailedText;
63
64
        return $this;
65
    }
66
67
    public function getValidationFailedText() : string
68
    {
69
        return $this->validationFailedText;
70
    }
71
72
    public function setPlaceholderText(string $placeholderText) : Input
73
    {
74
        $this->placeholderText = $placeholderText;
75
76
        return $this;
77
    }
78
79
    public function getPlaceholderText() : string
80
    {
81
        return $this->placeholderText;
82
    }
83
84
    public function setValidator(callable $validator) : Input
85
    {
86
        $this->validator = $validator;
87
        
88
        return $this;
89
    }
90
91
    public function ask() : InputResult
92
    {
93
        return $this->inputIO->collect($this);
94
    }
95
96
    public function validate(string $input) : bool
97
    {
98
        if ($this->validator) {
99
            $validator = $this->validator;
100
            
101
            if ($validator instanceof \Closure) {
102
                $validator = $validator->bindTo($this);
103
            }
104
            
105
            return $validator($input);
106
        }
107
108
        return !empty($input);
109
    }
110
111
    public function filter(string $value) : string
112
    {
113
        return $value;
114
    }
115
116
    public function getStyle() : MenuStyle
117
    {
118
        return $this->style;
119
    }
120
}
121