|
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->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; |
|
|
|
|
|
|
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, |
|
|
|
|
|
|
60
|
|
|
$this->exists ? static::getUpdatingRules($this) : static::getCreatingRules(), static::getMessages()); |
|
|
|
|
|
|
61
|
|
|
if ($v->passes()) { |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
$this->setErrors($v->messages()); |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set error message bag |
|
71
|
|
|
* |
|
72
|
|
|
* @var Illuminate\Support\MessageBag |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function setErrors($errors) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->errors = $errors; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retrieve error message bag |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getErrors() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->errors !== null ? $this->errors : []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Inverse of wasSaved |
|
89
|
|
|
*/ |
|
90
|
|
|
public function hasErrors() |
|
91
|
|
|
{ |
|
92
|
|
|
return !empty($this->errors); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return true if the validation is passed and the model was saved on db |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function wasSaved() |
|
100
|
|
|
{ |
|
101
|
|
|
return empty($this->errors); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
public static function getRules() |
|
106
|
|
|
{ |
|
107
|
|
|
if (isset(static::$rules)) { |
|
108
|
|
|
return static::$rules; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return []; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function getCreatingRules() |
|
115
|
|
|
{ |
|
116
|
|
|
if (isset(static::$rules)) { |
|
117
|
|
|
return static::$rules; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected static function replacePlaceholders(Model $model, $rules) |
|
124
|
|
|
{ |
|
125
|
|
|
$replaced = []; |
|
126
|
|
|
foreach ($rules as $key => $rule) { |
|
127
|
|
|
foreach ($model->attributes as $attr => $val) { |
|
128
|
|
|
$rule = str_replace('{' . $attr . '}', $val, $rule); |
|
129
|
|
|
} |
|
130
|
|
|
$replaced[$key] = $rule; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $replaced; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public static function getUpdatingRules(Model $model) |
|
137
|
|
|
{ |
|
138
|
|
|
$rules = []; |
|
139
|
|
|
|
|
140
|
|
|
if (isset(static::$rules)) { |
|
141
|
|
|
$rules = static::$rules; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (isset(static::$updating_rules)) { |
|
145
|
|
|
$rules = static::$updating_rules; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return static::replacePlaceholders($model, $rules); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public static function getMessages() |
|
152
|
|
|
{ |
|
153
|
|
|
if (isset(static::$messages)) { |
|
154
|
|
|
return static::$messages; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return []; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
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..