-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
49 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |