These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) Padosoft.com 2018. |
||
4 | */ |
||
5 | |||
6 | namespace Padosoft\Laravel\Validable; |
||
7 | |||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | use Illuminate\Support\Facades\App; |
||
10 | use Illuminate\Validation\Factory as ValidatorFactory; |
||
11 | |||
12 | /** |
||
13 | * Trait Validable |
||
14 | * @package Padosoft\Laravel\Validable |
||
15 | * @property Array $rules Validation rules |
||
16 | * @property Array $messages Validation messages |
||
17 | */ |
||
18 | trait Validable |
||
19 | { |
||
20 | /** |
||
21 | * Error message bag |
||
22 | * |
||
23 | * @var Illuminate\Support\MessageBag |
||
24 | */ |
||
25 | protected $errors; |
||
26 | /** |
||
27 | * Validator instance |
||
28 | * |
||
29 | * @var Illuminate\Validation\Validators |
||
30 | */ |
||
31 | protected $validator = null; |
||
32 | |||
33 | protected static function bootValidable() |
||
34 | { |
||
35 | static::saving(function (Model $model) { |
||
36 | if (!$model->hasValidator()) { |
||
37 | $model->setValidator(App::make('validator')); |
||
38 | } |
||
39 | |||
40 | return $model->validate(); |
||
41 | }); |
||
42 | } |
||
43 | |||
44 | public function setValidator(ValidatorFactory $validator) |
||
45 | { |
||
46 | $this->validator = $validator; |
||
0 ignored issues
–
show
|
|||
47 | } |
||
48 | |||
49 | public function hasValidator() |
||
50 | { |
||
51 | return $this->validator !== null; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Validates current attributes against rules |
||
56 | */ |
||
57 | public function validate() |
||
58 | { |
||
59 | $v = $this->validator->make($this->attributes, static::getRules(), static::getMessages()); |
||
60 | if ($v->passes()) { |
||
61 | return true; |
||
62 | } |
||
63 | $this->setErrors($v->messages()); |
||
64 | |||
65 | return false; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Set error message bag |
||
70 | * |
||
71 | * @var Illuminate\Support\MessageBag |
||
72 | */ |
||
73 | protected function setErrors($errors) |
||
74 | { |
||
75 | $this->errors = $errors; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Retrieve error message bag |
||
80 | */ |
||
81 | public function getErrors() |
||
82 | { |
||
83 | return $this->errors!==null?$this->errors:[]; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Inverse of wasSaved |
||
88 | */ |
||
89 | public function hasErrors() |
||
90 | { |
||
91 | return !empty($this->errors); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Return true if the validation is passed and the model was saved on db |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function wasSaved(){ |
||
99 | return empty($this->errors); |
||
100 | } |
||
101 | |||
102 | |||
103 | public static function getRules() |
||
104 | { |
||
105 | if (isset(static::$rules)) { |
||
106 | return static::$rules; |
||
107 | } |
||
108 | |||
109 | return []; |
||
110 | } |
||
111 | |||
112 | public static function getMessages() |
||
113 | { |
||
114 | if (isset(static::$messages)) { |
||
115 | return static::$messages; |
||
116 | } |
||
117 | |||
118 | return []; |
||
119 | } |
||
120 | } |
||
121 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..