Completed
Push — develop ( 0c1a18...5d4a6e )
by Abdelrahman
04:37
created

Contact::scopeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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.

Loading history...
309
                    ->withPivot('relation')->withTimestamps();
310
    }
311
312
    /**
313
     * A contact may be related to multiple contacts.
314
     *
315
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
316
     */
317
    public function backRelatives(): BelongsToMany
318
    {
319
        return $this->belongsToMany(self::class, config('rinvex.contacts.tables.contact_relations'), 'related_id', 'contact_id')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

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.

Loading history...
320
                    ->withPivot('relation')->withTimestamps();
321
    }
322
}
323