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