PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Modules / Shipping / Http / Requests / ShippingZoneRequest.php

ShippingZoneRequest.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Modules/Shipping/Http/Requests/ShippingZoneRequest.php

121 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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