Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
15 / 15 |
| ValidatesAttributes | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
15 / 15 |
| bootValidatesAttributes | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| getValidator | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| getValidatesOnSave | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getValidationRules | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getValidationMessages | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getValidationCustomAttributes | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| validate | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Helium\Illuminate2\Database\Eloquent\Concerns; |
| 4 | |
| 5 | use Illuminate\Contracts\Validation\Validator as ValidatorContract; |
| 6 | use Illuminate\Database\Eloquent\Model; |
| 7 | use Illuminate\Support\Facades\Validator as ValidatorFacade; |
| 8 | |
| 9 | /** |
| 10 | * @mixin Model |
| 11 | */ |
| 12 | trait ValidatesAttributes |
| 13 | { |
| 14 | /** |
| 15 | * Boot the trait. |
| 16 | * |
| 17 | * @return void |
| 18 | */ |
| 19 | public static function bootValidatesAttributes() |
| 20 | { |
| 21 | static::saving(function ($model) { |
| 22 | if ($model->getValidatesOnSave()) { |
| 23 | $model->validate(); |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get Validator instance. |
| 30 | * |
| 31 | * @return \Illuminate\Contracts\Validation\Validator |
| 32 | */ |
| 33 | public function getValidator(): ValidatorContract |
| 34 | { |
| 35 | return ValidatorFacade::make( |
| 36 | $this->getAttributes(), |
| 37 | $this->getValidationRules(), |
| 38 | $this->getValidationMessages(), |
| 39 | $this->getValidationCustomAttributes() |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get validates on save. |
| 45 | * |
| 46 | * @return bool |
| 47 | */ |
| 48 | public function getValidatesOnSave(): bool |
| 49 | { |
| 50 | return $this->validatesOnSave ?? true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get validation rules. |
| 55 | * |
| 56 | * @return array |
| 57 | */ |
| 58 | public function getValidationRules(): array |
| 59 | { |
| 60 | return $this->validationRules ?? []; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get validation messages. |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | public function getValidationMessages(): array |
| 69 | { |
| 70 | return $this->validationMessages ?? []; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get validation custom attributes. |
| 75 | * |
| 76 | * @return array |
| 77 | */ |
| 78 | public function getValidationCustomAttributes(): array |
| 79 | { |
| 80 | return $this->validationCustomAttributes ?? []; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Validate model attributes. |
| 85 | * |
| 86 | * @return array |
| 87 | * @throws \Illuminate\Validation\ValidationException |
| 88 | */ |
| 89 | public function validate(): array |
| 90 | { |
| 91 | return $this->getValidator()->validate(); |
| 92 | } |
| 93 | } |