File tree Expand file tree Collapse file tree 3 files changed +32
-7
lines changed Expand file tree Collapse file tree 3 files changed +32
-7
lines changed Original file line number Diff line number Diff line change 2222 "php" : " >=7.1" ,
2323 "beberlei/assert" : " ^2.4 | ^3" ,
2424 "php-school/terminal" : " ^0.2.1" ,
25- "ext-posix" : " *"
25+ "ext-posix" : " *" ,
26+ "ext-mbstring" : " *"
2627 },
2728 "autoload" : {
2829 "psr-4" : {
Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ class Password implements Input
3939 */
4040 private $ style ;
4141
42+ /**
43+ * @var int
44+ */
45+ private $ passwordLength = 16 ;
46+
4247 public function __construct (InputIO $ inputIO , MenuStyle $ style )
4348 {
4449 $ this ->inputIO = $ inputIO ;
@@ -84,7 +89,7 @@ public function getPlaceholderText() : string
8489 public function setValidator (callable $ validator ) : Input
8590 {
8691 $ this ->validator = $ validator ;
87-
92+
8893 return $ this ;
8994 }
9095
@@ -97,15 +102,15 @@ public function validate(string $input) : bool
97102 {
98103 if ($ this ->validator ) {
99104 $ validator = $ this ->validator ;
100-
105+
101106 if ($ validator instanceof \Closure) {
102107 $ validator = $ validator ->bindTo ($ this );
103108 }
104-
109+
105110 return $ validator ($ input );
106111 }
107112
108- return mb_strlen ($ input ) >= 16 ;
113+ return mb_strlen ($ input ) >= $ this -> passwordLength ;
109114 }
110115
111116 public function filter (string $ value ) : string
@@ -117,4 +122,9 @@ public function getStyle() : MenuStyle
117122 {
118123 return $ this ->style ;
119124 }
125+
126+ public function setPasswordLength (int $ length ) : int
127+ {
128+ return $ this ->passwordLength = $ length ;
129+ }
120130}
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ class PasswordTest extends TestCase
2525 private $ inputIO ;
2626
2727 /**
28- * @var Text
28+ * @var Password
2929 */
3030 private $ input ;
3131
@@ -131,9 +131,23 @@ public function testWithCustomValidatorAndCustomValidationMessage() : void
131131 };
132132
133133 $ this ->input ->setValidator ($ customValidate );
134-
134+
135135 self ::assertTrue ($ this ->input ->validate ('superstrongpassword ' ));
136136 self ::assertFalse ($ this ->input ->validate ('mypassword ' ));
137137 self ::assertEquals ('Password too generic ' , $ this ->input ->getValidationFailedText ());
138138 }
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+ }
139153}
You can’t perform that action at this time.
0 commit comments