File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,11 @@ class Number implements Input
30
30
*/
31
31
private $ placeholderText = '' ;
32
32
33
+ /**
34
+ * @var null|callable
35
+ */
36
+ private $ validator ;
37
+
33
38
/**
34
39
* @var MenuStyle
35
40
*/
@@ -77,6 +82,13 @@ public function getPlaceholderText() : string
77
82
return $ this ->placeholderText ;
78
83
}
79
84
85
+ public function setValidator (callable $ validator ) : Input
86
+ {
87
+ $ this ->validator = $ validator ;
88
+
89
+ return $ this ;
90
+ }
91
+
80
92
public function ask () : InputResult
81
93
{
82
94
$ this ->inputIO ->registerControlCallback (InputCharacter::UP , function (string $ input ) {
@@ -92,6 +104,16 @@ public function ask() : InputResult
92
104
93
105
public function validate (string $ input ) : bool
94
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
+
95
117
return (bool ) preg_match ('/^-?\d+$/ ' , $ input );
96
118
}
97
119
Original file line number Diff line number Diff line change @@ -29,6 +29,11 @@ class Text implements Input
29
29
*/
30
30
private $ placeholderText = '' ;
31
31
32
+ /**
33
+ * @var null|callable
34
+ */
35
+ private $ validator ;
36
+
32
37
/**
33
38
* @var MenuStyle
34
39
*/
@@ -76,13 +81,30 @@ public function getPlaceholderText() : string
76
81
return $ this ->placeholderText ;
77
82
}
78
83
84
+ public function setValidator (callable $ validator ) : Input
85
+ {
86
+ $ this ->validator = $ validator ;
87
+
88
+ return $ this ;
89
+ }
90
+
79
91
public function ask () : InputResult
80
92
{
81
93
return $ this ->inputIO ->collect ($ this );
82
94
}
83
95
84
96
public function validate (string $ input ) : bool
85
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
+
86
108
return !empty ($ input );
87
109
}
88
110
You can’t perform that action at this time.
0 commit comments