| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Shipping\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
class ShippingMethodRequest extends RequestGuard |
| 9 |
{ |
| 10 |
|
| 11 |
public function beforeValidation() |
| 12 |
{ |
| 13 |
$data = $this->all(); |
| 14 |
$data['is_enabled'] = (string)(Arr::get($data, 'is_enabled', 0)) === '1' ? 1 : 0; |
| 15 |
return $data; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
public function rules() |
| 22 |
{ |
| 23 |
return [ |
| 24 |
'zone_id' => 'required', |
| 25 |
'title' => 'required|string|maxLength:192', |
| 26 |
'amount' => 'nullable', |
| 27 |
'type' => 'required|string|maxLength:192', |
| 28 |
'settings' => 'nullable|array', |
| 29 |
'states' => 'nullable|array', |
| 30 |
'is_enabled' => 'nullable|integer|in:0,1', |
| 31 |
'method_id' => 'nullable|integer', |
| 32 |
'meta' => 'nullable|array', |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public function messages() |
| 40 |
{ |
| 41 |
return [ |
| 42 |
'zone_id.required' => esc_html__('Shipping zone is required.', 'fluent-cart'), |
| 43 |
'title.required' => esc_html__('Shipping method title is required.', 'fluent-cart'), |
| 44 |
'type.required' => esc_html__('Shipping method type is required.', 'fluent-cart') |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function sanitize() |
| 52 |
{ |
| 53 |
return [ |
| 54 |
'zone_id' => 'intval', |
| 55 |
'title' => 'sanitize_text_field', |
| 56 |
'type' => 'sanitize_text_field', |
| 57 |
'amount' => 'sanitize_text_field', |
| 58 |
'is_enabled' => 'sanitize_text_field', |
| 59 |
'method_id' => 'intval', |
| 60 |
'states' => function ($value) { |
| 61 |
if (is_array($value)) { |
| 62 |
return array_map('sanitize_text_field', $value); |
| 63 |
} |
| 64 |
return $value; |
| 65 |
}, |
| 66 |
'settings.configure_rate' => 'sanitize_text_field', |
| 67 |
'settings.class_aggregation' => 'sanitize_text_field', |
| 68 |
'settings.weight_tiers' => function ($value) { |
| 69 |
if (!is_array($value)) { |
| 70 |
return []; |
| 71 |
} |
| 72 |
return array_map(function ($tier) { |
| 73 |
return [ |
| 74 |
'min' => floatval($tier['min'] ?? 0), |
| 75 |
'max' => floatval($tier['max'] ?? 0), |
| 76 |
'cost' => floatval($tier['cost'] ?? 0), |
| 77 |
]; |
| 78 |
}, $value); |
| 79 |
}, |
| 80 |
'meta' => function ($value) { |
| 81 |
if (!is_array($value)) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
$sanitized = []; |
| 86 |
foreach ($value as $key => $val) { |
| 87 |
if (is_string($val)) { |
| 88 |
$sanitized[$key] = sanitize_text_field($val); |
| 89 |
} |
| 90 |
} |
| 91 |
return $sanitized; |
| 92 |
} |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|