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