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\Factory |
30
|
|
|
*/ |
31
|
|
|
protected $validator = null; |
32
|
|
|
|
33
|
|
|
protected static function bootValidable() |
34
|
|
|
{ |
35
|
|
|
static::saving(function (Model $model) { |
36
|
|
|
if (!$model->validate()){ |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setValidator(ValidatorFactory $validator) |
43
|
|
|
{ |
44
|
|
|
$this->validator = $validator; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function hasValidator() |
48
|
|
|
{ |
49
|
|
|
return $this->validator !== null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validates current attributes against rules |
54
|
|
|
*/ |
55
|
|
|
public function validate() |
56
|
|
|
{ |
57
|
|
|
if (!$this->hasValidator()) { |
58
|
|
|
$this->setValidator(App::make('validator')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$v = $this->validator->make($this->attributes, |
|
|
|
|
62
|
|
|
$this->exists ? static::getUpdatingRules($this) : static::getCreatingRules(), static::getMessages()); |
|
|
|
|
63
|
|
|
if ($v->passes()) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
$this->setErrors($v->messages()); |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set error message bag |
73
|
|
|
* |
74
|
|
|
* @var Illuminate\Support\MessageBag |
75
|
|
|
*/ |
76
|
|
|
protected function setErrors($errors) |
77
|
|
|
{ |
78
|
|
|
$this->errors = $errors; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieve error message bag |
83
|
|
|
*/ |
84
|
|
|
public function getErrors() |
85
|
|
|
{ |
86
|
|
|
return $this->errors !== null ? $this->errors : []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Inverse of wasSaved |
91
|
|
|
*/ |
92
|
|
|
public function hasErrors() |
93
|
|
|
{ |
94
|
|
|
return !empty($this->errors); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return true if the validation is passed and the model was saved on db |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function wasSaved() |
102
|
|
|
{ |
103
|
|
|
return empty($this->errors); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
public static function getRules() |
108
|
|
|
{ |
109
|
|
|
if (isset(static::$rules)) { |
110
|
|
|
return static::$rules; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function getCreatingRules() |
117
|
|
|
{ |
118
|
|
|
if (isset(static::$rules)) { |
119
|
|
|
return static::$rules; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected static function replacePlaceholders(Model $model, $rules) |
126
|
|
|
{ |
127
|
|
|
$replaced = []; |
128
|
|
|
foreach ($rules as $key => $rule) { |
129
|
|
|
foreach ($model->attributes as $attr => $val) { |
130
|
|
|
if(strpos($rule,$attr)!==false && is_scalar($val)){ |
131
|
|
|
$rule = str_replace('{' . $attr . '}', $val, $rule); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
$replaced[$key] = $rule; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $replaced; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function getUpdatingRules(Model $model) |
141
|
|
|
{ |
142
|
|
|
$rules = []; |
143
|
|
|
|
144
|
|
|
if (isset(static::$rules)) { |
145
|
|
|
$rules = static::$rules; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (isset(static::$updating_rules)) { |
149
|
|
|
$rules = static::$updating_rules; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return static::replacePlaceholders($model, $rules); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public static function getMessages() |
156
|
|
|
{ |
157
|
|
|
if (isset(static::$messages)) { |
158
|
|
|
return static::$messages; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return []; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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..