| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Status; |
| 6 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
class UpdateSubscriptionRequest extends RequestGuard |
| 10 |
{ |
| 11 |
public function beforeValidation() |
| 12 |
{ |
| 13 |
return Arr::get($this->all(), 'data', []); |
| 14 |
} |
| 15 |
|
| 16 |
public function rules() |
| 17 |
{ |
| 18 |
$allowedStatuses = [ |
| 19 |
Status::SUBSCRIPTION_ACTIVE, |
| 20 |
Status::SUBSCRIPTION_PAUSED, |
| 21 |
Status::SUBSCRIPTION_TRIALING, |
| 22 |
Status::SUBSCRIPTION_CANCELED, |
| 23 |
Status::SUBSCRIPTION_EXPIRED, |
| 24 |
Status::SUBSCRIPTION_COMPLETED, |
| 25 |
Status::SUBSCRIPTION_PAST_DUE, |
| 26 |
]; |
| 27 |
|
| 28 |
return [ |
| 29 |
'recurring_total' => 'nullable|numeric|min:0', |
| 30 |
'bill_times' => 'nullable|numeric|min:0', |
| 31 |
'billing_interval' => 'nullable|sanitizeText|maxLength:100', |
| 32 |
'status' => ['required', 'sanitizeText', 'validStatus' => function ($attribute, $value) use ($allowedStatuses) { |
| 33 |
if (!in_array($value, $allowedStatuses, true)) { |
| 34 |
return __('Invalid subscription status.', 'fluent-cart'); |
| 35 |
} |
| 36 |
return null; |
| 37 |
}], |
| 38 |
'next_billing_date' => ['nullable', 'sanitizeText', 'maxLength:25', 'validDate' => function ($attribute, $value) { |
| 39 |
if ($value && strtotime($value) === false) { |
| 40 |
return __('Invalid date format for next billing date.', 'fluent-cart'); |
| 41 |
} |
| 42 |
return null; |
| 43 |
}], |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
public function messages() |
| 48 |
{ |
| 49 |
return [ |
| 50 |
'recurring_total.numeric' => esc_html__('Recurring total must be a number.', 'fluent-cart'), |
| 51 |
'recurring_total.min' => esc_html__('Recurring total cannot be negative.', 'fluent-cart'), |
| 52 |
'bill_times.numeric' => esc_html__('Bill times must be a number.', 'fluent-cart'), |
| 53 |
'bill_times.min' => esc_html__('Bill times cannot be negative.', 'fluent-cart'), |
| 54 |
'status.required' => esc_html__('Subscription status is required.', 'fluent-cart'), |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
public function sanitize() |
| 59 |
{ |
| 60 |
return [ |
| 61 |
'recurring_total' => 'floatval', |
| 62 |
'bill_times' => 'intval', |
| 63 |
'billing_interval' => 'sanitize_text_field', |
| 64 |
'status' => 'sanitize_text_field', |
| 65 |
'next_billing_date' => 'sanitize_text_field', |
| 66 |
]; |
| 67 |
} |
| 68 |
} |
| 69 |
|