73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Fo\Orders;
|
|
|
|
use App\Enums\Order\PaymentModeEnum;
|
|
use App\Models\Shop\Order;
|
|
use App\Models\Shop\PaymentMode;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
|
|
|
class CheckoutRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return \auth('frontend')->check() and Gate::check('orderThings', Order::class);
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation()
|
|
{
|
|
$mode = null;
|
|
try {
|
|
$mode = PaymentModeEnum::make(\intval($this->mode));
|
|
} catch (\TypeError | \BadMethodCallException $e) {
|
|
$mode = null;
|
|
}
|
|
$this->merge([
|
|
'modeEnum' => $mode
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'mode' => [
|
|
'required', new EnumIsValidRule(PaymentModeEnum::class),
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
if (!$this->modeEnum or !Gate::check('isAvailable', [PaymentMode::class, $this->modeEnum])) {
|
|
$fail(\trans('Ce mode de paiement n\'est pas disponible.'));
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'mode' => trans('moyen de paiement')
|
|
];
|
|
}
|
|
}
|