Skip to content

Commit

Permalink
fix (#28)
Browse files Browse the repository at this point in the history
* fix

* fix

Co-authored-by: KD <do>
  • Loading branch information
KrzysztofDziedziechEscolasoft authored Mar 11, 2022
1 parent c61283a commit 7b79674
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Http/Requests/QuestionnaireAssignUnassignRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use EscolaLms\Questionnaire\Models\Questionnaire;
use EscolaLms\Questionnaire\Models\QuestionnaireModelType;
use EscolaLms\Questionnaire\Rules\ClassExist;
use EscolaLms\Questionnaire\Rules\ModelExist;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Exists;

class QuestionnaireAssignUnassignRequest extends FormRequest
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public function rules(): array
],
'model_id' => [
'integer',
new Exists($this->getQuestionnaireModelType()->model_class, 'id'),
new ModelExist($this->input('model_type_title'), 'id'),
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Requests/QuestionnaireFrontAnswerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use EscolaLms\Questionnaire\Models\Questionnaire;
use EscolaLms\Questionnaire\Models\QuestionnaireModelType;
use EscolaLms\Questionnaire\Rules\ClassExist;
use EscolaLms\Questionnaire\Rules\ModelExist;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Exists;

/**
* @OA\Schema(
Expand Down Expand Up @@ -72,7 +72,7 @@ public function rules(): array
],
'model_id' => [
'integer',
new Exists($this->getQuestionnaireModelType()->model_class, 'id'),
new ModelExist($this->input('model_type_title'), 'id'),
],
'answers' => ['sometimes', 'array'],
'answers.*' => ['sometimes', 'array'],
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Requests/QuestionnaireFrontReadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use EscolaLms\Questionnaire\Models\Questionnaire;
use EscolaLms\Questionnaire\Models\QuestionnaireModelType;
use EscolaLms\Questionnaire\Rules\ClassExist;
use EscolaLms\Questionnaire\Rules\ModelExist;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Exists;

class QuestionnaireFrontReadRequest extends FormRequest
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public function rules(): array
],
'model_id' => [
'integer',
new Exists($this->getQuestionnaireModelType()->model_class, 'id'),
new ModelExist($this->input('model_type_title'), 'id'),
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Requests/QuestionnaireStarsFrontRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use EscolaLms\Questionnaire\Models\QuestionnaireModelType;
use EscolaLms\Questionnaire\Rules\ClassExist;
use EscolaLms\Questionnaire\Rules\ModelExist;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Exists;

class QuestionnaireStarsFrontRequest extends FormRequest
{
Expand Down Expand Up @@ -34,7 +34,7 @@ public function rules(): array
],
'model_id' => [
'integer',
new Exists($this->getQuestionnaireModelType()->model_class, 'id'),
new ModelExist($this->input('model_type_title'), 'id'),
],
];
}
Expand Down
41 changes: 41 additions & 0 deletions src/Rules/ModelExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace EscolaLms\Questionnaire\Rules;

use EscolaLms\Questionnaire\Models\QuestionnaireModelType;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Database\Eloquent\Model;

class ModelExist implements Rule
{
private string $model;
private string $column;

public function __construct(string $modelTypeTitle, ?string $column = 'id')
{
$this->model = $this->getQuestionnaireModelType($modelTypeTitle)->model_class;
$this->column = $column;
}

public function passes($attribute, $value): bool
{
if (is_subclass_of($this->model, Model::class)) {
$model = new $this->model();
if ($model::find($value, $this->column)) {
return true;
}
}

return false;
}

public function message(): string
{
return 'The :attribute do not exist';
}

public function getQuestionnaireModelType(string $modelTypeTitle): QuestionnaireModelType
{
return QuestionnaireModelType::query()->where('title', $modelTypeTitle)->firstOrFail();
}
}

0 comments on commit 7b79674

Please sign in to comment.