2021-08-11 17:25:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Api\V2;
|
|
|
|
|
|
|
|
|
|
use App\Models\Answer;
|
|
|
|
|
use App\Models\Question;
|
2023-09-06 17:24:53 +02:00
|
|
|
use App\Models\Service;
|
2021-08-11 17:25:24 +02:00
|
|
|
|
2023-09-06 17:24:53 +02:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
2021-08-11 17:25:24 +02:00
|
|
|
class StoreCustomerAnswers extends BaseApiRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Resolve Service.
|
2023-09-06 17:24:53 +02:00
|
|
|
$service = app()->make(Service::class);
|
2021-08-11 17:25:24 +02:00
|
|
|
$brandId = $service->brand->id;
|
2023-09-06 17:24:53 +02:00
|
|
|
$question = Question::query()->findOrFail($this->question);
|
2021-08-11 17:25:24 +02:00
|
|
|
$questionTableName = (new Question())->getTable();
|
|
|
|
|
$answerTableName = (new Answer())->getTable();
|
|
|
|
|
return [
|
2023-09-04 13:08:34 +02:00
|
|
|
'question' => [
|
2021-08-11 17:25:24 +02:00
|
|
|
'required',
|
|
|
|
|
"exists:$questionTableName,id",
|
2024-09-09 15:26:35 +02:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
2021-08-11 17:25:24 +02:00
|
|
|
function ($attribute, $value, $fail) use ($question, $brandId) {
|
|
|
|
|
if ($question->brand->id !== $brandId) {
|
|
|
|
|
$fail("$attribute does not belong to brand $brandId");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
2023-09-04 13:08:34 +02:00
|
|
|
'answers' => 'required|array',
|
2021-08-11 17:25:24 +02:00
|
|
|
'answers.*' => [
|
|
|
|
|
'int',
|
|
|
|
|
'distinct',
|
|
|
|
|
"exists:$answerTableName,id",
|
|
|
|
|
function ($attribute, $value, $fail) use ($question) {
|
2023-09-06 17:24:53 +02:00
|
|
|
if (Answer::query()->findOrFail($value)->question->id !== $question->id) {
|
2021-08-11 17:25:24 +02:00
|
|
|
$fail("$attribute does not belong to question $question->id");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|