| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Services\Renderer\CheckoutFieldsSchema; |
| 7 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class CustomerAddressRequest extends RequestGuard |
| 11 |
{ |
| 12 |
public function beforeValidation() |
| 13 |
{ |
| 14 |
$data = $this->all(); |
| 15 |
$data['status'] = Arr::get($data, 'status', 'active'); |
| 16 |
$data['is_primary'] = Arr::get($data, 'is_primary', 0); |
| 17 |
return $data; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public function rules() |
| 24 |
{ |
| 25 |
$validationRules = App::localization()->getValidationRule($this->all(), null, [ |
| 26 |
'country' => 'required|sanitizeText' |
| 27 |
]); |
| 28 |
|
| 29 |
$rules = array_merge($validationRules, [ |
| 30 |
'label' => 'nullable|sanitizeText|maxLength:15', |
| 31 |
'name' => 'required|sanitizeText|maxLength:255', |
| 32 |
'email' => 'required|sanitizeText|email|maxLength:255', |
| 33 |
'address_1' => 'required|sanitizeText', |
| 34 |
'address_2' => 'nullable|sanitizeText', |
| 35 |
'city' => 'required|sanitizeText|maxLength:255', |
| 36 |
'is_primary' => 'nullable|numeric', |
| 37 |
'company_name' => 'nullable|sanitizeText|maxLength:255', |
| 38 |
'vat_number' => 'nullable|sanitizeText|maxLength:255', |
| 39 |
'legal_registration_id' => 'nullable|sanitizeText|maxLength:255', |
| 40 |
]); |
| 41 |
|
| 42 |
if (Arr::get($this->all(), 'type') === 'billing') { |
| 43 |
if (CheckoutFieldsSchema::isCompanyNameEnabled() && CheckoutFieldsSchema::isCompanyNameRequired()) { |
| 44 |
$rules['company_name'] = 'required|sanitizeText|maxLength:255'; |
| 45 |
} |
| 46 |
|
| 47 |
if (CheckoutFieldsSchema::isVatNumberEnabled() && CheckoutFieldsSchema::isVatNumberRequired()) { |
| 48 |
$rules['vat_number'] = 'required|sanitizeText|maxLength:255'; |
| 49 |
} |
| 50 |
|
| 51 |
if (CheckoutFieldsSchema::isLegalRegistrationIdEnabled() && CheckoutFieldsSchema::isLegalRegistrationIdRequired()) { |
| 52 |
$rules['legal_registration_id'] = 'required|sanitizeText|maxLength:255'; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $rules; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function messages() |
| 63 |
{ |
| 64 |
return [ |
| 65 |
'name.required' => esc_html__('Name field is required.', 'fluent-cart'), |
| 66 |
'email.required' => esc_html__('Email field is required.', 'fluent-cart'), |
| 67 |
'email.email' => esc_html__('Email must be a valid email address.', 'fluent-cart'), |
| 68 |
'address_1.required' => esc_html__('Street Address field is required.', 'fluent-cart'), |
| 69 |
'city.required' => esc_html__('City field is required.', 'fluent-cart'), |
| 70 |
'postcode.required' => esc_html__('Postcode field is required.', 'fluent-cart'), |
| 71 |
'country.required' => esc_html__('Country field is required.', 'fluent-cart'), |
| 72 |
'state.required' => esc_html__('State field is required.', 'fluent-cart'), |
| 73 |
'label.max' => esc_html__('Label may not be greater than 15 characters.', 'fluent-cart'), |
| 74 |
'company_name.required' => esc_html__('Company Name field is required.', 'fluent-cart'), |
| 75 |
'vat_number.required' => esc_html__('VAT/GST/Tax Number field is required.', 'fluent-cart'), |
| 76 |
'legal_registration_id.required' => esc_html__('Legal Registration ID field is required.', 'fluent-cart'), |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function sanitize() |
| 84 |
{ |
| 85 |
return [ |
| 86 |
'label' => 'sanitize_text_field', |
| 87 |
'type' => 'sanitize_text_field', |
| 88 |
'status' => 'sanitize_text_field', |
| 89 |
'name' => 'sanitize_text_field', |
| 90 |
'address_1' => 'sanitize_text_field', |
| 91 |
'address_2' => 'sanitize_text_field', |
| 92 |
'city' => 'sanitize_text_field', |
| 93 |
'state' => 'sanitize_text_field', |
| 94 |
'phone' => 'sanitize_text_field', |
| 95 |
'postcode' => 'sanitize_text_field', |
| 96 |
'country' => 'sanitize_text_field', |
| 97 |
'email' => function ($value) { |
| 98 |
if(empty($value)) { |
| 99 |
return ''; |
| 100 |
} |
| 101 |
|
| 102 |
return sanitize_email($value); |
| 103 |
}, |
| 104 |
'is_primary' => 'intval', |
| 105 |
'company_name' => 'sanitize_text_field', |
| 106 |
'vat_number' => 'sanitize_text_field', |
| 107 |
'legal_registration_id' => 'sanitize_text_field', |
| 108 |
]; |
| 109 |
} |
| 110 |
} |
| 111 |
|