48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Api\V4;
|
|
|
|
use App\Models\Answer;
|
|
use App\Models\Question;
|
|
use App\Models\Service;
|
|
|
|
class StoreCustomerAnswers extends BaseApiRequest
|
|
{
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
// * Resolve Service.
|
|
$service = app()->make(Service::class);
|
|
$brandId = $service->brand->id;
|
|
$question = Question::query()->findOrFail($this->question);
|
|
$questionTableName = (new Question())->getTable();
|
|
$answerTableName = (new Answer())->getTable();
|
|
return [
|
|
'question' => [
|
|
'required',
|
|
"exists:$questionTableName,id",
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function ($attribute, $value, $fail) use ($question, $brandId) {
|
|
if ($question->brand->id !== $brandId) {
|
|
$fail("$attribute does not belong to brand $brandId");
|
|
}
|
|
}
|
|
],
|
|
'answers' => 'required|array',
|
|
'answers.*' => [
|
|
'int',
|
|
'distinct',
|
|
"exists:$answerTableName,id",
|
|
function ($attribute, $value, $fail) use ($question) {
|
|
if (Answer::query()->findOrFail($value)->question->id !== $question->id) {
|
|
$fail("$attribute does not belong to question $question->id");
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
}
|