File tree Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Original file line number Diff line number Diff line change 22
22
"php" : " >=7.1" ,
23
23
"beberlei/assert" : " ^2.4 | ^3" ,
24
24
"php-school/terminal" : " ^0.2.1" ,
25
- "ext-posix" : " *"
25
+ "ext-posix" : " *" ,
26
+ "ext-mbstring" : " *"
26
27
},
27
28
"autoload" : {
28
29
"psr-4" : {
Original file line number Diff line number Diff line change @@ -28,4 +28,6 @@ public function getPlaceholderText() : string;
28
28
public function filter (string $ value ) : string ;
29
29
30
30
public function getStyle () : MenuStyle ;
31
+
32
+ public function setValidator (callable $ validator ) : Input ;
31
33
}
Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ class Password implements Input
39
39
*/
40
40
private $ style ;
41
41
42
+ /**
43
+ * @var int
44
+ */
45
+ private $ passwordLength = 16 ;
46
+
42
47
public function __construct (InputIO $ inputIO , MenuStyle $ style )
43
48
{
44
49
$ this ->inputIO = $ inputIO ;
@@ -84,7 +89,7 @@ public function getPlaceholderText() : string
84
89
public function setValidator (callable $ validator ) : Input
85
90
{
86
91
$ this ->validator = $ validator ;
87
-
92
+
88
93
return $ this ;
89
94
}
90
95
@@ -97,15 +102,15 @@ public function validate(string $input) : bool
97
102
{
98
103
if ($ this ->validator ) {
99
104
$ validator = $ this ->validator ;
100
-
105
+
101
106
if ($ validator instanceof \Closure) {
102
107
$ validator = $ validator ->bindTo ($ this );
103
108
}
104
-
109
+
105
110
return $ validator ($ input );
106
111
}
107
112
108
- return mb_strlen ($ input ) >= 16 ;
113
+ return mb_strlen ($ input ) >= $ this -> passwordLength ;
109
114
}
110
115
111
116
public function filter (string $ value ) : string
@@ -117,4 +122,9 @@ public function getStyle() : MenuStyle
117
122
{
118
123
return $ this ->style ;
119
124
}
125
+
126
+ public function setPasswordLength (int $ length ) : int
127
+ {
128
+ return $ this ->passwordLength = $ length ;
129
+ }
120
130
}
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ class PasswordTest extends TestCase
25
25
private $ inputIO ;
26
26
27
27
/**
28
- * @var Text
28
+ * @var Password
29
29
*/
30
30
private $ input ;
31
31
@@ -131,9 +131,23 @@ public function testWithCustomValidatorAndCustomValidationMessage() : void
131
131
};
132
132
133
133
$ this ->input ->setValidator ($ customValidate );
134
-
134
+
135
135
self ::assertTrue ($ this ->input ->validate ('superstrongpassword ' ));
136
136
self ::assertFalse ($ this ->input ->validate ('mypassword ' ));
137
137
self ::assertEquals ('Password too generic ' , $ this ->input ->getValidationFailedText ());
138
138
}
139
+
140
+ public function testPasswordValidationWithDefaultLength () : void
141
+ {
142
+ self ::assertFalse ($ this ->input ->validate (str_pad ('a ' , 15 )));
143
+ self ::assertTrue ($ this ->input ->validate (str_pad ('a ' , 16 )));
144
+ }
145
+
146
+ public function testPasswordValidationWithDefinedLength () : void
147
+ {
148
+ $ this ->input ->setPasswordLength (5 );
149
+
150
+ self ::assertFalse ($ this ->input ->validate (str_pad ('a ' , 4 )));
151
+ self ::assertTrue ($ this ->input ->validate (str_pad ('a ' , 5 )));
152
+ }
139
153
}
You can’t perform that action at this time.
0 commit comments