1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models\Validators; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\App; |
5
|
|
|
use Illuminate\Support\Facades\Validator as FacadeValidator; |
6
|
|
|
use Illuminate\Validation\Rule; |
7
|
|
|
use Illuminate\Validation\Validator; |
8
|
|
|
|
9
|
|
|
class UserValidator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get the validator for an incoming registration request. |
13
|
|
|
* |
14
|
|
|
* @param array $data The data to validate. |
15
|
|
|
* |
16
|
|
|
* @return \Illuminate\Validation\Validator |
17
|
|
|
*/ |
18
|
|
|
public static function create(array $data): Validator |
19
|
|
|
{ |
20
|
|
|
$rules = [ |
21
|
|
|
'username' => 'required|alpha_num|min:4|max:20|unique:users', |
22
|
|
|
'email' => 'required|email|max:50|unique:users', |
23
|
|
|
'password' => 'required|min:6|confirmed', |
24
|
|
|
'terms' => 'required|min:1' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
// Bipass the captcha for the unit testing. |
28
|
|
|
if (App::environment() !== 'testing') { |
29
|
|
|
$rules = array_merge($rules, ['g-recaptcha-response' => 'required|recaptcha']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return FacadeValidator::make($data, $rules); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the validator for an incoming registration request with a social provider. |
37
|
|
|
* |
38
|
|
|
* @param array $data The data to validate. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Validation\Validator |
41
|
|
|
*/ |
42
|
|
|
public static function createWithProvider(array $data): Validator |
43
|
|
|
{ |
44
|
|
|
$rules = [ |
45
|
|
|
'username' => 'required|alpha_num|min:4|max:20|unique:users', |
46
|
|
|
'email' => 'required|email|max:50|unique:users' |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
return FacadeValidator::make($data, $rules); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a validator for an incoming update request. (Administration) |
54
|
|
|
* |
55
|
|
|
* @param array $data The data to validate. |
56
|
|
|
* @param int $id The actual user id to ignore the username rule. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Validation\Validator |
59
|
|
|
*/ |
60
|
|
|
public static function update(array $data, int $id): Validator |
61
|
|
|
{ |
62
|
|
|
$rules = [ |
63
|
|
|
'username' => [ |
64
|
|
|
'required', |
65
|
|
|
'alpha_num', |
66
|
|
|
'min:4', |
67
|
|
|
'max:20', |
68
|
|
|
Rule::unique('users')->ignore($id) |
69
|
|
|
], |
70
|
|
|
'email' => [ |
71
|
|
|
'required', |
72
|
|
|
'email', |
73
|
|
|
'max:50', |
74
|
|
|
Rule::unique('users')->ignore($id) |
75
|
|
|
], |
76
|
|
|
'account.first_name' => 'max:100', |
77
|
|
|
'account.last_name' => 'max:100', |
78
|
|
|
'account.facebook' => 'max:50', |
79
|
|
|
'account.twitter' => 'max:50', |
80
|
|
|
'roles' => 'required' |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
return FacadeValidator::make($data, $rules); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the validator for an incoming email update request. |
88
|
|
|
* |
89
|
|
|
* @param array $data The data to validate. |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Validation\Validator |
92
|
|
|
*/ |
93
|
|
|
public static function updateEmail(array $data): Validator |
94
|
|
|
{ |
95
|
|
|
$rules = [ |
96
|
|
|
'email' => 'required|email|max:50|unique:users' |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return FacadeValidator::make($data, $rules); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the validator for an incoming password update request. |
104
|
|
|
* |
105
|
|
|
* @param array $data The data to validate. |
106
|
|
|
* |
107
|
|
|
* @return \Illuminate\Validation\Validator |
108
|
|
|
*/ |
109
|
|
|
public static function updatePassword(array $data): Validator |
110
|
|
|
{ |
111
|
|
|
$rules = [ |
112
|
|
|
'oldpassword' => 'required', |
113
|
|
|
'password' => 'required|min:6|confirmed' |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
return FacadeValidator::make($data, $rules); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|