|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests\Customer; |
|
4
|
|
|
|
|
5
|
|
|
use App\Enum\Gender; |
|
6
|
|
|
use App\Infrastructure\Foundation\Http\FormRequest; |
|
7
|
|
|
use App\Models\Customer; |
|
8
|
|
|
use Illuminate\Support\Facades\Validator; |
|
9
|
|
|
use Illuminate\Validation\Rule; |
|
10
|
|
|
use Propaganistas\LaravelPhone\PhoneNumber; |
|
11
|
|
|
|
|
12
|
|
|
class UpdateRequest extends FormRequest |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritDoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function getRules() |
|
18
|
|
|
{ |
|
19
|
|
|
$phoneRule = function () { |
|
20
|
|
|
return ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) { |
|
21
|
|
|
$validator = Validator::make(compact('phone'), [ |
|
22
|
|
|
'phone' => Rule::unique(Customer::class)->where(function ($query) use ($phone) { |
|
23
|
|
|
$query->where('phone', PhoneNumber::make($phone, 'ID')->formatE164()); |
|
24
|
|
|
})->ignore($phone, 'phone'), |
|
25
|
|
|
]); |
|
26
|
|
|
|
|
27
|
|
|
if ($validator->fails()) { |
|
28
|
|
|
$fail(trans('validation.unique', compact('attribute'))); |
|
29
|
|
|
} |
|
30
|
|
|
}]; |
|
31
|
|
|
}; |
|
32
|
|
|
|
|
33
|
|
|
return [ |
|
34
|
|
|
'username' => 'required|string|max:255', |
|
35
|
|
|
'fullname' => 'required|string|max:255', |
|
36
|
|
|
'gender' => ['sometimes', 'nullable', Rule::in(Gender::toValues())], |
|
37
|
|
|
'email' => ['required', 'string', 'email', 'max:255', function ($attribute, $email, $fail) { |
|
38
|
|
|
$validator = Validator::make(compact('email'), [ |
|
39
|
|
|
'email' => Rule::unique(Customer::class)->ignore($email, 'email'), |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
if ($validator->fails()) { |
|
43
|
|
|
$fail(trans('validation.unique', compact('attribute'))); |
|
44
|
|
|
} |
|
45
|
|
|
}], |
|
46
|
|
|
'phone_country' => 'sometimes|in:ID', |
|
47
|
|
|
'phone' => value($phoneRule), |
|
48
|
|
|
'whatsapp_phone_country' => 'sometimes|in:ID', |
|
49
|
|
|
'whatsapp_phone' => value($phoneRule), |
|
50
|
|
|
'account_number' => 'required_without:identitycard_number|numeric', |
|
51
|
|
|
'identitycard_number' => 'required_without:account_number|numeric', |
|
52
|
|
|
'identitycard_image' => 'sometimes|nullable|image', |
|
53
|
|
|
'location_latitude' => 'sometimes|nullable|numeric', |
|
54
|
|
|
'location_longitude' => 'sometimes|nullable|numeric', |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getAttributes() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
'username' => trans('Username'), |
|
65
|
|
|
'fullname' => trans('Full name'), |
|
66
|
|
|
'gender' => trans('Gender'), |
|
67
|
|
|
'email' => trans('Email'), |
|
68
|
|
|
'phone_country' => trans('Phone Country'), |
|
69
|
|
|
'phone' => trans('Phone Number'), |
|
70
|
|
|
'whatsapp_phone_country' => trans('Whatsapp Phone Country'), |
|
71
|
|
|
'whatsapp_phone' => trans('Whatsapp Phone Number'), |
|
72
|
|
|
'account_number' => trans('Account Number'), |
|
73
|
|
|
'identitycard_number' => trans('Identity Card Number'), |
|
74
|
|
|
'identitycard_image' => trans('Identity Card Image'), |
|
75
|
|
|
'location_latitude' => trans('Latitude'), |
|
76
|
|
|
'location_longitude' => trans('Longitude'), |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|