| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Shipping\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\Framework\Validator\ValidationException; |
| 9 |
|
| 10 |
class ShippingZoneRequest extends RequestGuard |
| 11 |
{ |
| 12 |
|
| 13 |
public function beforeValidation() |
| 14 |
{ |
| 15 |
$data = $this->all(); |
| 16 |
$data['region'] = Arr::get($data, 'region', ''); |
| 17 |
$data['order'] = Arr::get($data, 'order', ''); |
| 18 |
|
| 19 |
// Only include shipping_class_id if explicitly submitted — prevents wiping on edit |
| 20 |
if (array_key_exists('shipping_class_id', $data)) { |
| 21 |
$classId = $data['shipping_class_id']; |
| 22 |
$data['shipping_class_id'] = $classId ? intval($classId) : null; |
| 23 |
} else { |
| 24 |
// Omitted on update means "preserve the stored class". Resolve the |
| 25 |
// effective class HERE (this return is merged into the request) so |
| 26 |
// the whole-world uniqueness rule below validates the same class |
| 27 |
// bucket the row will actually keep — and the controller writes |
| 28 |
// that same value back. Without this, a class-scoped zone going |
| 29 |
// region=all was checked against general zones: falsely rejected by |
| 30 |
// an unrelated general whole-world zone, while a real conflict in |
| 31 |
// its own class went unchecked. |
| 32 |
$currentId = Arr::get($data, 'id') ?: App::getInstance()->request->get('id'); |
| 33 |
if ($currentId) { |
| 34 |
$storedClassId = \FluentCart\App\Models\ShippingZone::query() |
| 35 |
->where('id', intval($currentId)) |
| 36 |
->value('shipping_class_id'); |
| 37 |
$data['shipping_class_id'] = $storedClassId ? intval($storedClassId) : null; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return $data; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function rules() |
| 48 |
{ |
| 49 |
|
| 50 |
return [ |
| 51 |
'name' => 'required|string|maxLength:192', |
| 52 |
'region' => function ($attr, $value) { |
| 53 |
if ($value === 'all') { |
| 54 |
$shippingClassId = Arr::get($this->all(), 'shipping_class_id', null); |
| 55 |
$query = \FluentCart\App\Models\ShippingZone::query()->where('region', 'all'); |
| 56 |
if ($shippingClassId) { |
| 57 |
$query->where('shipping_class_id', $shippingClassId); |
| 58 |
} else { |
| 59 |
$query->where(function ($q) { |
| 60 |
$q->whereNull('shipping_class_id') |
| 61 |
->orWhere('shipping_class_id', 0); |
| 62 |
}); |
| 63 |
} |
| 64 |
// Exclude current zone on update |
| 65 |
$currentId = Arr::get($this->all(), 'id') ?: App::getInstance()->request->get('id'); |
| 66 |
if ($currentId) { |
| 67 |
$query->where('id', '!=', intval($currentId)); |
| 68 |
} |
| 69 |
if ($query->first()) { |
| 70 |
return __('Only one "Whole World" shipping zone is allowed.', 'fluent-cart'); |
| 71 |
} |
| 72 |
} |
| 73 |
return null; |
| 74 |
}, |
| 75 |
'order' => 'nullable|integer', |
| 76 |
'shipping_class_id' => 'nullable|integer', |
| 77 |
'meta' => 'nullable|array', |
| 78 |
'meta.countries' => 'nullable|array', |
| 79 |
'meta.selection_type' => 'nullable|string|in:included,excluded', |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function messages() |
| 87 |
{ |
| 88 |
return [ |
| 89 |
'name.required' => esc_html__('Shipping name is required.', 'fluent-cart'), |
| 90 |
'name.max' => esc_html__('Shipping name cannot exceed 192 characters.', 'fluent-cart'), |
| 91 |
'region.required' => esc_html__('Shipping country region is required.', 'fluent-cart') |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function sanitize() |
| 99 |
{ |
| 100 |
return [ |
| 101 |
'name' => 'sanitize_text_field', |
| 102 |
'region' => 'sanitize_text_field', |
| 103 |
'order' => 'intval', |
| 104 |
'shipping_class_id' => function ($value) { |
| 105 |
return $value ? intval($value) : null; |
| 106 |
}, |
| 107 |
'meta' => function ($value) { |
| 108 |
if (!is_array($value)) return []; |
| 109 |
$sanitized = []; |
| 110 |
if (isset($value['countries']) && is_array($value['countries'])) { |
| 111 |
$sanitized['countries'] = array_map('sanitize_text_field', $value['countries']); |
| 112 |
} |
| 113 |
if (isset($value['selection_type'])) { |
| 114 |
$sanitized['selection_type'] = sanitize_text_field($value['selection_type']); |
| 115 |
} |
| 116 |
return $sanitized; |
| 117 |
} |
| 118 |
]; |
| 119 |
} |
| 120 |
} |
| 121 |
|