Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StoreCustomerAnswers | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| rules | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Requests\Api\V2; |
| 4 | |
| 5 | use App\Models\Answer; |
| 6 | use App\Models\Question; |
| 7 | |
| 8 | class StoreCustomerAnswers extends BaseApiRequest |
| 9 | { |
| 10 | /** |
| 11 | * Get the validation rules that apply to the request. |
| 12 | * |
| 13 | * @return array |
| 14 | */ |
| 15 | public function rules(): array |
| 16 | { |
| 17 | // * Resolve Service. |
| 18 | $service = app()->make(\App\Models\Service::class); |
| 19 | $brandId = $service->brand->id; |
| 20 | $question = Question::findOrFail($this->question); |
| 21 | $questionTableName = (new Question())->getTable(); |
| 22 | $answerTableName = (new Answer())->getTable(); |
| 23 | return [ |
| 24 | 'question' => [ |
| 25 | 'required', |
| 26 | "exists:$questionTableName,id", |
| 27 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed |
| 28 | function ($attribute, $value, $fail) use ($question, $brandId) { |
| 29 | if ($question->brand->id !== $brandId) { |
| 30 | $fail("$attribute does not belong to brand $brandId"); |
| 31 | } |
| 32 | } |
| 33 | ], |
| 34 | 'answers' => 'required|array', |
| 35 | 'answers.*' => [ |
| 36 | 'int', |
| 37 | 'distinct', |
| 38 | "exists:$answerTableName,id", |
| 39 | function ($attribute, $value, $fail) use ($question) { |
| 40 | if (Answer::findOrFail($value)->question->id !== $question->id) { |
| 41 | $fail("$attribute does not belong to question $question->id"); |
| 42 | } |
| 43 | } |
| 44 | ] |
| 45 | ]; |
| 46 | } |
| 47 | } |