PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.26
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.26
1.6.6 1.6.5 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 All 49 releases
fluent-cart / app / Modules / Shipping / Http / Requests / ShippingZoneRequest.php

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

105 lines 3.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 }
24
25 return $data;
26 }
27
28 /**
29 * @return array
30 */
31 public function rules()
32 {
33
34 return [
35 'name' => 'required|string|maxLength:192',
36 'region' => function ($attr, $value) {
37 if ($value === 'all') {
38 $shippingClassId = Arr::get($this->all(), 'shipping_class_id', null);
39 $query = \FluentCart\App\Models\ShippingZone::query()->where('region', 'all');
40 if ($shippingClassId) {
41 $query->where('shipping_class_id', $shippingClassId);
42 } else {
43 $query->where(function ($q) {
44 $q->whereNull('shipping_class_id')
45 ->orWhere('shipping_class_id', 0);
46 });
47 }
48 // Exclude current zone on update
49 $currentId = Arr::get($this->all(), 'id') ?: App::getInstance()->request->get('id');
50 if ($currentId) {
51 $query->where('id', '!=', intval($currentId));
52 }
53 if ($query->first()) {
54 return __('Only one "Whole World" shipping zone is allowed.', 'fluent-cart');
55 }
56 }
57 return null;
58 },
59 'order' => 'nullable|integer',
60 'shipping_class_id' => 'nullable|integer',
61 'meta' => 'nullable|array',
62 'meta.countries' => 'nullable|array',
63 'meta.selection_type' => 'nullable|string|in:included,excluded',
64 ];
65 }
66
67 /**
68 * @return array
69 */
70 public function messages()
71 {
72 return [
73 'name.required' => esc_html__('Shipping name is required.', 'fluent-cart'),
74 'name.max' => esc_html__('Shipping name cannot exceed 192 characters.', 'fluent-cart'),
75 'region.required' => esc_html__('Shipping country region is required.', 'fluent-cart')
76 ];
77 }
78
79 /**
80 * @return array
81 */
82 public function sanitize()
83 {
84 return [
85 'name' => 'sanitize_text_field',
86 'region' => 'sanitize_text_field',
87 'order' => 'intval',
88 'shipping_class_id' => function ($value) {
89 return $value ? intval($value) : null;
90 },
91 'meta' => function ($value) {
92 if (!is_array($value)) return [];
93 $sanitized = [];
94 if (isset($value['countries']) && is_array($value['countries'])) {
95 $sanitized['countries'] = array_map('sanitize_text_field', $value['countries']);
96 }
97 if (isset($value['selection_type'])) {
98 $sanitized['selection_type'] = sanitize_text_field($value['selection_type']);
99 }
100 return $sanitized;
101 }
102 ];
103 }
104 }
105