1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Donor entity class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @subpackage Models |
7
|
|
|
* @since 1.0 |
8
|
|
|
*/ |
9
|
|
|
namespace EBloodBank\Models; |
10
|
|
|
|
11
|
|
|
use DateTime; |
12
|
|
|
use DateTimeZone; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use EBloodBank as EBB; |
15
|
|
|
use EBloodBank\Traits\EntityMeta; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Donor entity class |
19
|
|
|
* |
20
|
|
|
* @since 1.0 |
21
|
|
|
* |
22
|
|
|
* @Entity(repositoryClass="EBloodBank\Models\DonorRepository") |
23
|
|
|
* @Table(name="donor") |
24
|
|
|
* @HasLifecycleCallbacks |
25
|
|
|
*/ |
26
|
|
|
class Donor extends Entity |
27
|
|
|
{ |
28
|
|
|
use EntityMeta; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Donor ID |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
* @since 1.0 |
35
|
|
|
* |
36
|
|
|
* @Id |
37
|
|
|
* @GeneratedValue |
38
|
|
|
* @Column(type="integer", name="donor_id") |
39
|
|
|
*/ |
40
|
|
|
protected $id = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Donor name |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
* @since 1.0 |
47
|
|
|
* |
48
|
|
|
* @Column(type="string", name="donor_name") |
49
|
|
|
*/ |
50
|
|
|
protected $name; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Donor gender |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
* @since 1.0 |
57
|
|
|
* |
58
|
|
|
* @Column(type="string", name="donor_gender") |
59
|
|
|
*/ |
60
|
|
|
protected $gender; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Donor birthdate |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
* @since 1.0 |
67
|
|
|
* |
68
|
|
|
* @Column(type="string", name="donor_birthdate") |
69
|
|
|
*/ |
70
|
|
|
protected $birthdate; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Donor blood group |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
* @since 1.0 |
77
|
|
|
* |
78
|
|
|
* @Column(type="string", name="donor_blood_group") |
79
|
|
|
*/ |
80
|
|
|
protected $blood_group; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Donor district |
84
|
|
|
* |
85
|
|
|
* @var District |
86
|
|
|
* @since 1.0 |
87
|
|
|
* |
88
|
|
|
* @ManyToOne(targetEntity="EBloodBank\Models\District", inversedBy="donors") |
89
|
|
|
* @JoinColumn(name="donor_district_id", referencedColumnName="district_id") |
90
|
|
|
*/ |
91
|
|
|
protected $district; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Donor creation datetime |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
* @since 1.0 |
98
|
|
|
* |
99
|
|
|
* @Column(type="datetime", name="donor_created_at") |
100
|
|
|
*/ |
101
|
|
|
protected $created_at; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Donor created by |
105
|
|
|
* |
106
|
|
|
* @var User |
107
|
|
|
* @since 1.0 |
108
|
|
|
* |
109
|
|
|
* @ManyToOne(targetEntity="EBloodBank\Models\User") |
110
|
|
|
* @JoinColumn(name="donor_created_by", referencedColumnName="user_id") |
111
|
|
|
*/ |
112
|
|
|
protected $created_by; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Donor status |
116
|
|
|
* |
117
|
|
|
* @var string |
118
|
|
|
* @since 1.0 |
119
|
|
|
* |
120
|
|
|
* @Column(type="string", name="donor_status") |
121
|
|
|
*/ |
122
|
|
|
protected $status; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Donor meta |
126
|
|
|
* |
127
|
|
|
* @var array |
128
|
|
|
* @since 1.0 |
129
|
|
|
* |
130
|
|
|
* @Column(type="json", name="donor_meta") |
131
|
|
|
*/ |
132
|
|
|
protected $meta = []; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
* @since 1.0 |
137
|
|
|
*/ |
138
|
|
|
public function isExists() |
139
|
|
|
{ |
140
|
|
|
$id = (int) $this->get('id'); |
141
|
|
|
return ! empty($id); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
* @since 1.0 |
147
|
|
|
*/ |
148
|
|
|
public function isPending() |
149
|
|
|
{ |
150
|
|
|
return 'pending' === $this->get('status'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
155
|
|
|
* @since 1.0 |
156
|
|
|
*/ |
157
|
|
|
public function isApproved() |
158
|
|
|
{ |
159
|
|
|
return 'approved' === $this->get('status'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
* @since 1.0 |
165
|
|
|
*/ |
166
|
|
|
public function getGenderTitle() |
167
|
|
|
{ |
168
|
|
|
$genders = EBB\getGenders(); |
|
|
|
|
169
|
|
|
$gender = $this->get('gender'); |
170
|
|
|
if (isset($genders[$gender])) { |
171
|
|
|
$gender = $genders[$gender]; |
172
|
|
|
} |
173
|
|
|
return $gender; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return string |
178
|
|
|
* @since 1.0 |
179
|
|
|
*/ |
180
|
|
|
public function getAge($format = '%y') |
181
|
|
|
{ |
182
|
|
|
$currentDate = new DateTime(date('Y-m-d')); |
183
|
|
|
$birthdate = new DateTime($this->get('birthdate')); |
184
|
|
|
|
185
|
|
|
if ($birthdate > $currentDate) { |
186
|
|
|
return 0; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $currentDate->diff($birthdate)->format($format); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return void |
194
|
|
|
* @since 1.6 |
195
|
|
|
* |
196
|
|
|
* @PrePersist |
197
|
|
|
*/ |
198
|
|
|
public function doActionOnPrePersist() |
199
|
|
|
{ |
200
|
|
|
$this->set('created_at', new DateTime('now', new DateTimeZone('UTC'))); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return mixed |
205
|
|
|
* @since 1.0 |
206
|
|
|
* @static |
207
|
|
|
*/ |
208
|
|
|
public static function sanitize($key, $value) |
209
|
|
|
{ |
210
|
|
|
switch ($key) { |
211
|
|
|
case 'id': |
212
|
|
|
$value = EBB\sanitizeInteger($value); |
|
|
|
|
213
|
|
|
break; |
214
|
|
|
case 'name': |
215
|
|
|
$value = EBB\sanitizeTitle($value); |
|
|
|
|
216
|
|
|
break; |
217
|
|
|
case 'gender': |
218
|
|
|
case 'status': |
219
|
|
|
case 'birthdate': |
220
|
|
|
case 'blood_group': |
221
|
|
|
$value = EBB\sanitizeSlug($value); |
|
|
|
|
222
|
|
|
break; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $value; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @throws \InvalidArgumentException |
230
|
|
|
* @return bool |
231
|
|
|
* @since 1.0 |
232
|
|
|
* @static |
233
|
|
|
*/ |
234
|
|
|
public static function validate($key, $value) |
235
|
|
|
{ |
236
|
|
|
switch ($key) { |
237
|
|
|
case 'id': |
238
|
|
|
if (! EBB\isValidID($value)) { |
|
|
|
|
239
|
|
|
throw new InvalidArgumentException(__('Invalid donor ID.')); |
240
|
|
|
} |
241
|
|
|
break; |
242
|
|
|
case 'name': |
243
|
|
|
if (! is_string($value) || empty($value)) { |
244
|
|
|
throw new InvalidArgumentException(__('Invalid donor name.')); |
245
|
|
|
} |
246
|
|
|
break; |
247
|
|
|
case 'gender': |
248
|
|
|
if (! array_key_exists($value, EBB\getGenders())) { |
|
|
|
|
249
|
|
|
throw new InvalidArgumentException(__('Invalid donor gender.')); |
250
|
|
|
} |
251
|
|
|
break; |
252
|
|
|
case 'blood_group': |
253
|
|
|
if (! in_array($value, EBB\getBloodGroups(), true)) { |
|
|
|
|
254
|
|
|
throw new InvalidArgumentException(__('Invalid donor blood group.')); |
255
|
|
|
} |
256
|
|
|
break; |
257
|
|
|
case 'district': |
258
|
|
|
if (! $value instanceof District || ! $value->isExists()) { |
259
|
|
|
throw new InvalidArgumentException(__('Invalid donor district.')); |
260
|
|
|
} |
261
|
|
|
break; |
262
|
|
|
case 'created_by': |
263
|
|
|
if (! $value instanceof User || ! $value->isExists()) { |
264
|
|
|
throw new InvalidArgumentException(__('Invalid donor originator.')); |
265
|
|
|
} |
266
|
|
|
break; |
267
|
|
|
case 'status': |
268
|
|
|
if (! is_string($value) || empty($value)) { |
269
|
|
|
throw new InvalidArgumentException(__('Invalid donor status.')); |
270
|
|
|
} |
271
|
|
|
break; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return true; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return mixed |
279
|
|
|
* @since 1.0 |
280
|
|
|
* @static |
281
|
|
|
*/ |
282
|
|
|
public static function sanitizeMeta(string $key, $value) |
283
|
|
|
{ |
284
|
|
|
switch ($key) { |
285
|
|
|
case 'weight': |
286
|
|
|
$value = EBB\sanitizeFloat($value); |
|
|
|
|
287
|
|
|
break; |
288
|
|
|
case 'email': |
289
|
|
|
$value = EBB\sanitizeEmail($value); |
|
|
|
|
290
|
|
|
break; |
291
|
|
|
case 'phone': |
292
|
|
|
$value = EBB\sanitizeInteger($value); |
|
|
|
|
293
|
|
|
break; |
294
|
|
|
case 'address': |
295
|
|
|
$value = EBB\sanitizeTitle($value); |
|
|
|
|
296
|
|
|
break; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $value; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @throws \InvalidArgumentException |
304
|
|
|
* @return bool |
305
|
|
|
* @since 1.0 |
306
|
|
|
* @static |
307
|
|
|
*/ |
308
|
|
|
public static function validateMeta(string $key, $value) |
309
|
|
|
{ |
310
|
|
|
switch ($key) { |
311
|
|
|
case 'weight': |
312
|
|
|
if (! empty($value) && ! EBB\isValidFloat($value)) { |
|
|
|
|
313
|
|
|
// TODO: Check Min and Max weight. |
314
|
|
|
throw new InvalidArgumentException(__('Invalid donor weight.')); |
315
|
|
|
} |
316
|
|
|
break; |
317
|
|
|
case 'email': |
318
|
|
|
if (! empty($value) && ! EBB\isValidEmail($value)) { |
|
|
|
|
319
|
|
|
throw new InvalidArgumentException(__('Invalid donor e-mail address.')); |
320
|
|
|
} |
321
|
|
|
break; |
322
|
|
|
case 'email_visibility': |
323
|
|
|
if (! empty($value) && ! array_key_exists($value, EBB\getVisibilities())) { |
|
|
|
|
324
|
|
|
throw new InvalidArgumentException(__('Invalid donor e-mail address visibility.')); |
325
|
|
|
} |
326
|
|
|
break; |
327
|
|
|
case 'phone': |
328
|
|
|
if (! empty($value) && ! ctype_digit($value)) { |
329
|
|
|
throw new InvalidArgumentException(__('Invalid donor phone number.')); |
330
|
|
|
} |
331
|
|
|
break; |
332
|
|
|
case 'phone_visibility': |
333
|
|
|
if (! empty($value) && ! array_key_exists($value, EBB\getVisibilities())) { |
334
|
|
|
throw new InvalidArgumentException(__('Invalid donor phone number visibility.')); |
335
|
|
|
} |
336
|
|
|
break; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
return true; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|