| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests\FrontendRequests; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Services\Localization\LocalizationManager; |
| 7 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class CustomerAddressRequest extends RequestGuard |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public function rules() |
| 17 |
{ |
| 18 |
$data = $this->all(); |
| 19 |
$type = $this->get('type') . '_'; |
| 20 |
$prefix = $type === 'billing_' ? 'billing' : 'shipping'; |
| 21 |
$rules = App::localization()->getValidationRule($data, $prefix); |
| 22 |
|
| 23 |
return array_merge($rules, [ |
| 24 |
'type' => 'required|sanitizeText', |
| 25 |
$type . 'label' => 'required|sanitizeText|maxLength:15', |
| 26 |
$type . 'name' => 'nullable|sanitizeText|maxLength:255', |
| 27 |
$type . 'address_1' => 'required|sanitizeText', |
| 28 |
$type . 'address_2' => 'nullable|sanitizeText', |
| 29 |
$type . 'city' => 'required|sanitizeText|maxLength:255', |
| 30 |
]); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public function messages() |
| 37 |
{ |
| 38 |
$type = $this->get('type') . '_'; |
| 39 |
|
| 40 |
return [ |
| 41 |
$type . 'address_1.required' => esc_html__('Street Address field is required.', 'fluent-cart'), |
| 42 |
$type . 'label.max' => esc_html__('Label may not be greater than 15 characters.', 'fluent-cart'), |
| 43 |
$type . 'city.required' => esc_html__('City field is required.', 'fluent-cart'), |
| 44 |
$type . 'state.required' => esc_html__('State field is required.', 'fluent-cart'), |
| 45 |
$type . 'postcode.required' => esc_html__('Postcode field is required.', 'fluent-cart'), |
| 46 |
$type . 'country.required' => esc_html__('Country field is required.', 'fluent-cart'), |
| 47 |
$type . 'label.required' => esc_html__('Label field is required.', 'fluent-cart') |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function sanitize() |
| 55 |
{ |
| 56 |
$type = $this->get('type') . '_'; |
| 57 |
|
| 58 |
return [ |
| 59 |
'type' => 'sanitize_text_field', |
| 60 |
$type . 'status' => 'sanitize_text_field', |
| 61 |
$type . 'label' => 'sanitize_text_field', |
| 62 |
$type . 'name' => 'sanitize_text_field', |
| 63 |
$type . 'address_1' => 'sanitize_text_field', |
| 64 |
$type . 'address_2' => 'sanitize_text_field', |
| 65 |
$type . 'city' => 'sanitize_text_field', |
| 66 |
$type . 'state' => 'sanitize_text_field', |
| 67 |
$type . 'phone' => 'sanitize_text_field', |
| 68 |
$type . 'postcode' => 'sanitize_text_field', |
| 69 |
$type . 'country' => 'sanitize_text_field', |
| 70 |
$type . 'email' => function ($value) { |
| 71 |
if(empty($value)) { |
| 72 |
return ''; |
| 73 |
} |
| 74 |
|
| 75 |
return sanitize_email($value); |
| 76 |
}, |
| 77 |
]; |
| 78 |
} |
| 79 |
} |
| 80 |
|