PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.25
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.25
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 trunk All 48 releases
fluent-cart / app / Http / Requests / ProductCreateRequest.php

ProductCreateRequest.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.25, at app/Http/Requests/ProductCreateRequest.php

84 lines 2.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\Http\Requests;
4
5 use FluentCart\App\Models\ShippingClass;
6 use FluentCart\App\Services\DateTime\DateTime;
7 use FluentCart\Framework\Foundation\RequestGuard;
8 use FluentCart\Framework\Support\Arr;
9
10 class ProductCreateRequest extends RequestGuard
11 {
12 /**
13 * Prepare and normalize the incoming data before validation.
14 *
15 * This method ensures that each variant in the request payload has the necessary structure
16 * expected for processing, particularly focusing on the `other_info` attribute.
17 *
18 * If `other_info` is missing from a variant (common during data migration scenarios),
19 * this method sets default values to prevent validation or processing errors.
20 *
21 * It also ensures that:
22 * - `fulfillment_type` is consistently applied across all variants, defaulting to 'physical'.
23 * - `payment_type` is set (defaulting to 'onetime') and injected into `other_info`.
24 * - `other_info` is populated with a consistent structure containing default billing and setup fee options.
25 *
26 * @return array The normalized request data ready for validation.
27 */
28
29
30 /**
31 * @return array
32 */
33 public function rules(): array
34 {
35 return [
36 'post_title' => 'required|sanitizeText|maxLength:200',
37 'post_status' => ['nullable', 'string'],
38 'detail.fulfillment_type' => ['sanitizeText', function ($attribute, $value) {
39 if (!in_array($value, ['physical', 'digital'])) {
40 return __('Invalid fulfillment type.', 'fluent-cart');
41 }
42 return null;
43 }],
44 ];
45 }
46
47
48 /**
49 * @return array
50 */
51 public function messages(): array
52 {
53 return [
54 'post_title.required' => esc_html__('Title is required.', 'fluent-cart'),
55 'post_title.max' => esc_html__('Title may not be greater than 200 characters.', 'fluent-cart'),
56 'detail.fulfillment_type.required' => esc_html__('Fulfilment Type is required.', 'fluent-cart'),
57 ];
58 }
59
60
61 /**
62 * @return array
63 */
64 public function sanitize(): array
65 {
66
67 return [
68 'post_title' => 'sanitize_text_field',
69 'post_status' => 'sanitize_text_field',
70 'detail.fulfillment_type' => function ($value) {
71 if (empty($value)) {
72 return 'digital';
73 }
74 return sanitize_text_field($value);
75 },
76
77 'detail.other_info.is_bundle_product' => function ($value) {
78 return $value === 'yes' ? 'yes' : 'no';
79 }
80 ];
81
82 }
83 }
84