| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 6 |
|
| 7 |
class GroupBulkUpdateVariantRequest extends RequestGuard |
| 8 |
{ |
| 9 |
const MAX_VARIANTS_PER_REQUEST = 500; |
| 10 |
|
| 11 |
public function rules() |
| 12 |
{ |
| 13 |
$variantIds = $this->get('variant_ids', []); |
| 14 |
if (is_array($variantIds)) { |
| 15 |
$variantIds = array_values(array_filter(array_map('absint', $variantIds))); |
| 16 |
} |
| 17 |
$skuRule = 'nullable|sanitizeText|maxLength:30'; |
| 18 |
|
| 19 |
if (is_array($variantIds) && count($variantIds) === 1) { |
| 20 |
$excludeId = absint($variantIds[0]); |
| 21 |
$skuRule .= '|unique:fct_product_variations,sku,' . $excludeId; |
| 22 |
} |
| 23 |
|
| 24 |
return [ |
| 25 |
'variant_ids' => 'required|array', |
| 26 |
'sku' => $skuRule, |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function messages() |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'variant_ids.required' => esc_html__('At least one variant ID is required.', 'fluent-cart'), |
| 34 |
'variant_ids.array' => esc_html__('variant_ids must be an array.', 'fluent-cart'), |
| 35 |
'sku.maxLength' => esc_html__('SKU may not be greater than 30 characters.', 'fluent-cart'), |
| 36 |
'sku.unique' => esc_html__('The SKU must be unique.', 'fluent-cart'), |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function sanitize() |
| 41 |
{ |
| 42 |
return [ |
| 43 |
'variant_ids' => function ($value) { |
| 44 |
if (!is_array($value)) { |
| 45 |
return []; |
| 46 |
} |
| 47 |
return array_slice( |
| 48 |
array_values(array_filter(array_map('absint', $value))), |
| 49 |
0, |
| 50 |
self::MAX_VARIANTS_PER_REQUEST |
| 51 |
); |
| 52 |
}, |
| 53 |
'sku' => function ($value) { |
| 54 |
return ($value !== null && $value !== '') ? sanitize_text_field($value) : null; |
| 55 |
}, |
| 56 |
]; |
| 57 |
} |
| 58 |
} |
| 59 |
|