File tree Expand file tree Collapse file tree 3 files changed +68
-118
lines changed Expand file tree Collapse file tree 3 files changed +68
-118
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ namespace doganoo \PHPUtil \Service ;
5
+
6
+
7
+ class User {
8
+
9
+ public const PASSWORD_VALIDATION_UNSAFE = 1 ;
10
+ public const PASSWORD_VALIDATION_BASIC = 2 ;
11
+ public const PASSWORD_VALIDATION_SECURE = 3 ;
12
+
13
+ public function validEmail (string $ email ): bool {
14
+ return \filter_var ($ email , \FILTER_VALIDATE_EMAIL ) !== false ;
15
+ }
16
+
17
+ public function validatePassword (string $ password , int $ mode = self ::PASSWORD_VALIDATION_SECURE ): bool {
18
+
19
+ if ($ mode === User::PASSWORD_VALIDATION_UNSAFE ) {
20
+ return $ this ->validatePasswordUnsafe ($ password );
21
+ } else if ($ mode === User::PASSWORD_VALIDATION_BASIC ) {
22
+ return $ this ->validatePasswordBasic ($ password );
23
+ } else {
24
+ return $ this ->validatePasswordSecure ($ password );
25
+ }
26
+ }
27
+
28
+ private function validatePasswordUnsafe (string $ password ): bool {
29
+ return \strlen ($ password ) >= 8 ;
30
+ }
31
+
32
+ private function validatePasswordBasic (string $ password ): bool {
33
+ $ valid = $ this ->validatePasswordUnsafe ($ password );
34
+ $ valid = $ valid && ((bool )preg_match ("(^(?=.*[a-z])(?=.*[A-Z]).+$) " , $ password ));
35
+ return $ valid ;
36
+ }
37
+
38
+ private function validatePasswordSecure (string $ password ): bool {
39
+ $ valid = $ this ->validatePasswordUnsafe ($ password );
40
+ return $ valid && ((bool )preg_match ("(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$) " , $ password ));
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ use doganoo \PHPUtil \Service \User as UserService ;
5
+ use PHPUnit \Framework \TestCase ;
6
+
7
+ class User extends TestCase {
8
+
9
+ public function testValidMail () {
10
+ $ userService = new UserService ();
11
+ $ this ->assertTrue (true === $ userService ->validEmail ("test@test.de " ));
12
+ $ this ->assertTrue (false === $ userService ->validEmail ("test@test " ));
13
+ $ this ->assertTrue (false === $ userService ->validEmail ("phputil " ));
14
+ }
15
+
16
+ public function testValidPassword () {
17
+ $ userService = new UserService ();
18
+ $ this ->assertTrue (true === $ userService ->validatePassword ("doganoophputil " , UserService::PASSWORD_VALIDATION_UNSAFE ));
19
+ $ this ->assertTrue (false === $ userService ->validatePassword ("phputil " , UserService::PASSWORD_VALIDATION_UNSAFE ));
20
+ $ this ->assertTrue (true === $ userService ->validatePassword ("Doganoophputil " , UserService::PASSWORD_VALIDATION_BASIC ));
21
+ $ this ->assertTrue (false === $ userService ->validatePassword ("phputil " , UserService::PASSWORD_VALIDATION_BASIC ));
22
+ $ this ->assertTrue (true === $ userService ->validatePassword ("Doganoophputil2 " , UserService::PASSWORD_VALIDATION_SECURE ));
23
+ $ this ->assertTrue (false === $ userService ->validatePassword ("phputil " , UserService::PASSWORD_VALIDATION_SECURE ));
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments