| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Models\Customer; |
| 7 |
use FluentCart\App\Models\User; |
| 8 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 9 |
|
| 10 |
class AttachUserRequest extends RequestGuard |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public function rules(): array |
| 17 |
{ |
| 18 |
$userId = absint(App::request()->get('user_id')); |
| 19 |
$user = User::query()->with('customer')->find($userId); |
| 20 |
|
| 21 |
|
| 22 |
return [ |
| 23 |
'user_id' => [ |
| 24 |
'required', 'string', 'maxLength:50', |
| 25 |
function ($attribute, $value) use ($user) { |
| 26 |
|
| 27 |
if (empty($user)) { |
| 28 |
return __('User not found.', 'fluent-cart'); |
| 29 |
} |
| 30 |
|
| 31 |
if (!empty($user->customer)) { |
| 32 |
return (__('User already linked to a customer.', 'fluent-cart')); |
| 33 |
} |
| 34 |
return null; |
| 35 |
} |
| 36 |
], |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public function messages(): array |
| 44 |
{ |
| 45 |
return [ |
| 46 |
'user_id.required' => esc_html__('User is required.', 'fluent-cart'), |
| 47 |
'user_id.exists' => esc_html__('User not found.', 'fluent-cart'), |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function sanitize(): array |
| 55 |
{ |
| 56 |
return [ |
| 57 |
'user_id' => 'absint', |
| 58 |
]; |
| 59 |
} |
| 60 |
} |
| 61 |
|