| Total Complexity | 4 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class Unique implements Rule |
||
| 8 | { |
||
| 9 | protected string $model; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Create a new rule instance. |
||
| 13 | * |
||
| 14 | * @return void |
||
| 15 | */ |
||
| 16 | public function __construct(string $model) |
||
| 17 | { |
||
| 18 | $this->model = $model; |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Determine if the validation rule passes. |
||
| 23 | * |
||
| 24 | * @param string $attribute |
||
| 25 | * @param mixed $value |
||
| 26 | * @return bool |
||
| 27 | */ |
||
| 28 | public function passes($attribute, $value) |
||
| 29 | { |
||
| 30 | $model = $this->createModel(); |
||
| 31 | |||
| 32 | $instance = $model->findById($value); |
||
| 33 | |||
| 34 | return !$instance->exists(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get the validation error message. |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public function message() |
||
| 43 | { |
||
| 44 | return 'The model is not unique.'; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Create a new instance of the model. |
||
| 49 | * |
||
| 50 | * @return \AloiaCms\Models\Model |
||
| 51 | */ |
||
| 52 | private function createModel() |
||
| 57 | } |
||
| 58 | } |
||
| 59 |