57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Api\V1;
|
|
|
|
use App\Enums\CampaignTypes;
|
|
use App\Models\Campaign;
|
|
use Closure;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class PrintRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'customer_hashcode' => 'required|exists:customers,hashcode',
|
|
'campaign_id' => [
|
|
'required',
|
|
'exists:campaigns,id',
|
|
function (string $attribute, mixed $value, Closure $fail) {
|
|
$campaign = Campaign::find($value);
|
|
if ($campaign && $campaign->status === 0) {
|
|
$fail("The selected {$attribute} is invalid.");
|
|
}
|
|
},
|
|
],
|
|
'coupons' => [
|
|
Rule::requiredIf(function () {
|
|
$campaign = Campaign::find($this->campaign_id);
|
|
return $campaign->type === CampaignTypes::WALL ? true : false;
|
|
}),
|
|
function (string $attribute, mixed $value, Closure $fail) {
|
|
foreach (explode(",", $value) as $coupon_id) {
|
|
if (!is_int(intval($coupon_id)) or !intval($coupon_id) or intval($coupon_id) < 0) {
|
|
$fail("The selected {$attribute} is invalid.");
|
|
}
|
|
}
|
|
},
|
|
]
|
|
];
|
|
}
|
|
}
|