1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zurbaev\DataTransferObjects; |
4
|
|
|
|
5
|
|
|
use Zurbaev\DataTransferObjects\Exceptions\DtoMethodNotFoundException; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
abstract class DataTransferObject |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* DTO attributes. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $attributes = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DTO validation rules. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $rules = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Validation errors bag. |
27
|
|
|
* |
28
|
|
|
* @var \Illuminate\Support\MessageBag |
29
|
|
|
*/ |
30
|
|
|
protected $errors; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get DTO attribute value. |
34
|
|
|
* |
35
|
|
|
* @param $name |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function __get($name) |
40
|
|
|
{ |
41
|
|
|
if (!isset($this->attributes[$name])) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->attributes[$name]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set DTO attribute value. |
50
|
|
|
* |
51
|
|
|
* @param $name |
52
|
|
|
* @param $value |
53
|
|
|
*/ |
54
|
|
|
public function __set($name, $value) |
55
|
|
|
{ |
56
|
|
|
$this->attributes[$name] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check if DTO attribute exists. |
61
|
|
|
* |
62
|
|
|
* @param $name |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function __isset($name) |
67
|
|
|
{ |
68
|
|
|
return $this->hasAttribute($name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Call virtual getter or setter. |
73
|
|
|
* |
74
|
|
|
* @param $method |
75
|
|
|
* @param $arguments |
76
|
|
|
* |
77
|
|
|
* @throws DtoMethodNotFoundException |
78
|
|
|
* |
79
|
|
|
* @return $this|mixed |
80
|
|
|
*/ |
81
|
|
|
public function __call($method, $arguments) |
82
|
|
|
{ |
83
|
|
|
if (Str::startsWith($method, 'set')) { |
84
|
|
|
return $this->virtualSetter($method, $arguments); |
85
|
|
|
} elseif (Str::startsWith($method, 'get')) { |
86
|
|
|
return $this->virtualGetter($method); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw DtoMethodNotFoundException::make($this, $method); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Creates new DTO instance from given array. |
94
|
|
|
* |
95
|
|
|
* @param array $data |
96
|
|
|
* |
97
|
|
|
* @return static |
98
|
|
|
*/ |
99
|
|
|
public static function fromArray(array $data) |
100
|
|
|
{ |
101
|
|
|
$instance = new static; |
102
|
|
|
|
103
|
|
|
foreach ($data as $key => $value) { |
104
|
|
|
$instance->{$key} = $value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $instance; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $method |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
protected function virtualGetter(string $method) |
116
|
|
|
{ |
117
|
|
|
$attribute = Str::snake(Str::substr($method, 3)); |
118
|
|
|
|
119
|
|
|
return $this->getAttribute($attribute); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $method |
124
|
|
|
* @param array $arguments |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
protected function virtualSetter(string $method, array $arguments) |
129
|
|
|
{ |
130
|
|
|
$attribute = Str::snake(Str::substr($method, 3)); |
131
|
|
|
|
132
|
|
|
$this->{$attribute} = array_get($arguments, 0); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check if given attribute exists in current DTO. |
139
|
|
|
* |
140
|
|
|
* @param string $attribute |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function hasAttribute(string $attribute): bool |
145
|
|
|
{ |
146
|
|
|
return isset($this->attributes[$attribute]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check if given attribute exists in current DTO & has not-NULL value. |
151
|
|
|
* |
152
|
|
|
* @param string $attribute |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function hasNotNullAttribute(string $attribute): bool |
157
|
|
|
{ |
158
|
|
|
return $this->hasAttribute($attribute) && !is_null($this->attributes[$attribute]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get attribute value. |
163
|
|
|
* |
164
|
|
|
* @param string $attribute |
165
|
|
|
* |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
public function getAttribute(string $attribute) |
169
|
|
|
{ |
170
|
|
|
return $this->attributes[$attribute]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Validates DTO. |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function validate(): bool |
179
|
|
|
{ |
180
|
|
|
/** |
181
|
|
|
* @var \Illuminate\Validation\Validator $validator |
182
|
|
|
*/ |
183
|
|
|
$validator = Validator::make($this->attributes, $this->rules()); |
184
|
|
|
|
185
|
|
|
$result = $validator->passes(); |
186
|
|
|
|
187
|
|
|
if (!$result) { |
188
|
|
|
$this->errors = $validator->errors(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $result; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get validation errors bag. |
196
|
|
|
* |
197
|
|
|
* @return \Illuminate\Support\MessageBag |
198
|
|
|
*/ |
199
|
|
|
public function errors() |
200
|
|
|
{ |
201
|
|
|
return $this->errors; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Validation rules. |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function rules() |
210
|
|
|
{ |
211
|
|
|
return $this->rules; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set validation rules from external source. |
216
|
|
|
* |
217
|
|
|
* @param array $rules |
218
|
|
|
* |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function setRules(array $rules) |
222
|
|
|
{ |
223
|
|
|
$this->rules = $rules; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Merge existed validation rules with given rules. |
230
|
|
|
* |
231
|
|
|
* @param array $rules |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function mergeRules(array $rules) |
236
|
|
|
{ |
237
|
|
|
$this->rules = array_merge($this->rules, $rules); |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|