1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Contacts\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rinvex\Contacts\Models\Contact. |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property int $entity_id |
19
|
|
|
* @property string $entity_type |
20
|
|
|
* @property string $given_name |
21
|
|
|
* @property string $family_name |
22
|
|
|
* @property string $full_name |
23
|
|
|
* @property string $title |
24
|
|
|
* @property string $organization |
25
|
|
|
* @property string $email |
26
|
|
|
* @property string $phone |
27
|
|
|
* @property string $fax |
28
|
|
|
* @property string $country_code |
29
|
|
|
* @property string $language_code |
30
|
|
|
* @property string $birthday |
31
|
|
|
* @property string $gender |
32
|
|
|
* @property string $national_id_type |
33
|
|
|
* @property string $national_id |
34
|
|
|
* @property string $source |
35
|
|
|
* @property string $method |
36
|
|
|
* @property string $notes |
37
|
|
|
* @property \Carbon\Carbon|null $created_at |
38
|
|
|
* @property \Carbon\Carbon|null $updated_at |
39
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Contacts\Models\Contact[] $backRelatives |
40
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $entity |
41
|
|
|
* @property-read string $name |
42
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Contacts\Models\Contact[] $relatives |
43
|
|
|
* |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact country($countryCode) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact language($languageCode) |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact method($method) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact source($source) |
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereBirthday($value) |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereCountryCode($value) |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereCreatedAt($value) |
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereEmail($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereEntityId($value) |
53
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereEntityType($value) |
54
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereFax($value) |
55
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereFamilyName($value) |
56
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereGivenName($value) |
57
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereGender($value) |
58
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereId($value) |
59
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereLanguageCode($value) |
60
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereNationalId($value) |
61
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereNationalIdType($value) |
62
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereMethod($value) |
63
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereNotes($value) |
64
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereOrganization($value) |
65
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact wherePhone($value) |
66
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereSource($value) |
67
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereTitle($value) |
68
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Contacts\Models\Contact whereUpdatedAt($value) |
69
|
|
|
* @mixin \Eloquent |
70
|
|
|
*/ |
71
|
|
|
class Contact extends Model |
72
|
|
|
{ |
73
|
|
|
use ValidatingTrait; |
74
|
|
|
use CacheableEloquent; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected $fillable = [ |
80
|
|
|
'entity_id', |
81
|
|
|
'entity_type', |
82
|
|
|
'source', |
83
|
|
|
'method', |
84
|
|
|
'given_name', |
85
|
|
|
'family_name', |
86
|
|
|
'title', |
87
|
|
|
'organization', |
88
|
|
|
'email', |
89
|
|
|
'phone', |
90
|
|
|
'fax', |
91
|
|
|
'country_code', |
92
|
|
|
'language_code', |
93
|
|
|
'birthday', |
94
|
|
|
'gender', |
95
|
|
|
'national_id_type', |
96
|
|
|
'national_id', |
97
|
|
|
'source', |
98
|
|
|
'method', |
99
|
|
|
'notes', |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
protected $casts = [ |
106
|
|
|
'entity_id' => 'integer', |
107
|
|
|
'entity_type' => 'string', |
108
|
|
|
'given_name' => 'string', |
109
|
|
|
'family_name' => 'string', |
110
|
|
|
'title' => 'string', |
111
|
|
|
'organization' => 'string', |
112
|
|
|
'email' => 'string', |
113
|
|
|
'phone' => 'string', |
114
|
|
|
'fax' => 'string', |
115
|
|
|
'country_code' => 'string', |
116
|
|
|
'language_code' => 'string', |
117
|
|
|
'birthday' => 'string', |
118
|
|
|
'gender' => 'string', |
119
|
|
|
'national_id_type' => 'string', |
120
|
|
|
'national_id' => 'string', |
121
|
|
|
'source' => 'string', |
122
|
|
|
'method' => 'string', |
123
|
|
|
'notes' => 'string', |
124
|
|
|
'deleted_at' => 'datetime', |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
protected $observables = [ |
131
|
|
|
'validating', |
132
|
|
|
'validated', |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The default rules that the model will validate against. |
137
|
|
|
* |
138
|
|
|
* @var array |
139
|
|
|
*/ |
140
|
|
|
protected $rules = [ |
141
|
|
|
'entity_id' => 'required|integer', |
142
|
|
|
'entity_type' => 'required|string|max:150', |
143
|
|
|
'given_name' => 'required|string|max:150', |
144
|
|
|
'family_name' => 'nullable|string|max:150', |
145
|
|
|
'title' => 'nullable|string|max:150', |
146
|
|
|
'organization' => 'nullable|string|max:150', |
147
|
|
|
'email' => 'nullable|email|min:3|max:150', |
148
|
|
|
'phone' => 'nullable|numeric|phone', |
149
|
|
|
'fax' => 'nullable|string|max:150', |
150
|
|
|
'country_code' => 'nullable|alpha|size:2|country', |
151
|
|
|
'language_code' => 'nullable|alpha|size:2|language', |
152
|
|
|
'birthday' => 'nullable|date_format:Y-m-d', |
153
|
|
|
'gender' => 'nullable|in:male,female', |
154
|
|
|
'national_id_type' => 'nullable|in:identification,passport,other', |
155
|
|
|
'national_id' => 'nullable|string|max:150', |
156
|
|
|
'source' => 'nullable|string|max:150', |
157
|
|
|
'method' => 'nullable|string|max:150', |
158
|
|
|
'notes' => 'nullable|string|max:10000', |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Whether the model should throw a |
163
|
|
|
* ValidationException if it fails validation. |
164
|
|
|
* |
165
|
|
|
* @var bool |
166
|
|
|
*/ |
167
|
|
|
protected $throwValidationExceptions = true; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Create a new Eloquent model instance. |
171
|
|
|
* |
172
|
|
|
* @param array $attributes |
173
|
|
|
*/ |
174
|
|
|
public function __construct(array $attributes = []) |
175
|
|
|
{ |
176
|
|
|
parent::__construct($attributes); |
177
|
|
|
|
178
|
|
|
$this->setTable(config('rinvex.contacts.tables.contacts')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the owner model of the contact. |
183
|
|
|
* |
184
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
185
|
|
|
*/ |
186
|
|
|
public function entity(): MorphTo |
187
|
|
|
{ |
188
|
|
|
return $this->morphTo('entity', 'entity_type', 'entity_id'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Enforce clean sources. |
193
|
|
|
* |
194
|
|
|
* @param string $value |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function setSourceAttribute($value): void |
199
|
|
|
{ |
200
|
|
|
$this->attributes['source'] = str_slug($value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Enforce clean methods. |
205
|
|
|
* |
206
|
|
|
* @param string $value |
207
|
|
|
* |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
public function setMethodAttribute($value): void |
211
|
|
|
{ |
212
|
|
|
$this->attributes['method'] = str_slug($value); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Scope contacts by the given country. |
217
|
|
|
* |
218
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
219
|
|
|
* @param string $countryCode |
220
|
|
|
* |
221
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
222
|
|
|
*/ |
223
|
|
|
public function scopeCountry(Builder $builder, string $countryCode): Builder |
224
|
|
|
{ |
225
|
|
|
return $builder->where('country_code', $countryCode); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Scope contacts by the given language. |
230
|
|
|
* |
231
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
232
|
|
|
* @param string $languageCode |
233
|
|
|
* |
234
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
235
|
|
|
*/ |
236
|
|
|
public function scopeLanguage(Builder $builder, string $languageCode): Builder |
237
|
|
|
{ |
238
|
|
|
return $builder->where('language_code', $languageCode); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Scope contacts by the given source. |
243
|
|
|
* |
244
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
245
|
|
|
* @param string $source |
246
|
|
|
* |
247
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
248
|
|
|
*/ |
249
|
|
|
public function scopeSource(Builder $builder, string $source): Builder |
250
|
|
|
{ |
251
|
|
|
return $builder->where('source', $source); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Scope contacts by the given method. |
256
|
|
|
* |
257
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
258
|
|
|
* @param string $method |
259
|
|
|
* |
260
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
261
|
|
|
*/ |
262
|
|
|
public function scopeMethod(Builder $builder, string $method): Builder |
263
|
|
|
{ |
264
|
|
|
return $builder->where('method', $method); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get full name attribute. |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
public function getFullNameAttribute(): string |
273
|
|
|
{ |
274
|
|
|
return implode(' ', [$this->given_name, $this->family_name]); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* A contact may have multiple related contacts. |
279
|
|
|
* |
280
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
281
|
|
|
*/ |
282
|
|
|
public function relatives(): BelongsToMany |
283
|
|
|
{ |
284
|
|
|
return $this->belongsToMany(self::class, config('rinvex.contacts.tables.contact_relations'), 'contact_id', 'related_id') |
|
|
|
|
285
|
|
|
->withPivot('relation')->withTimestamps(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* A contact may be related to multiple contacts. |
290
|
|
|
* |
291
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
292
|
|
|
*/ |
293
|
|
|
public function backRelatives(): BelongsToMany |
294
|
|
|
{ |
295
|
|
|
return $this->belongsToMany(self::class, config('rinvex.contacts.tables.contact_relations'), 'related_id', 'contact_id') |
|
|
|
|
296
|
|
|
->withPivot('relation')->withTimestamps(); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.