Files
portal-api/app/Http/Requests/Api/Order/StoreOrderRequest.php
T

67 lines
2.5 KiB
PHP

<?php
namespace App\Http\Requests\Api\Order;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Support\BillingCycleCatalog;
use App\Enums\Product\ProductTypeEnums;
use App\Models\Product;
class StoreOrderRequest extends FormRequest
{
private const DOMAIN_PATTERN = '/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z]{2,63}$/i';
private function isHostingOrder(): bool
{
return Product::where('uuid', $this->input('product_id'))
->value('product_type') === ProductTypeEnums::HOSTING->value;
}
protected function prepareForValidation(): void
{
if (is_string($this->input('server_name'))) {
$this->merge(['server_name' => trim($this->input('server_name'))]);
}
}
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'product_id' => ['required', Rule::exists('products', 'uuid')->whereNull('deleted_at')],
'billing_cycle'=> ['required', Rule::in([...BillingCycleCatalog::codes(), 'onetime'])],
'server_name' => $this->isHostingOrder()
? ['required', 'string', 'max:253', 'regex:'.self::DOMAIN_PATTERN]
: 'nullable|string|max:255',
'os_id' => 'nullable|integer',
'coupon_code' => 'nullable|string|max:50',
'addons' => 'array',
'addons.*.addon_id' => 'nullable|exists:product_addon,id',
'addons.*.qty' => 'nullable|integer|min:0|max:1000000',
];
}
public function messages(): array
{
return [
'product_id.required' => 'Vui lòng chọn sản phẩm',
'product_id.exists' => 'Sản phẩm không tồn tại',
'billing_cycle.required' => 'Vui lòng chọn chu kỳ thanh toán',
'billing_cycle.enum' => 'Chu kỳ thanh toán không hợp lệ',
'billing_cycle.exists' => 'Chu kỳ thanh toán không áp dụng cho sản phẩm này',
'server_name.required' => 'Vui lòng nhập tên miền cho hosting',
'server_name.regex' => 'Tên miền không hợp lệ (ví dụ: example.com)',
'os_id.exists' => 'Hệ điều hành không hợp lệ',
'addons.*.addon_id.exists' => 'Addon không tồn tại',
'addons.*.qty.min' => 'Số lượng addon phải lớn hơn 0',
'addons.*.qty.max' => 'Số lượng addon vượt quá giới hạn cho phép',
];
}
}