Completed
Push — develop ( 9db14c...3f0e2c )
by Abdelrahman
02:51
created

Contact::scopeSource()   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                                                                  $created_at
42
 * @property \Carbon\Carbon                                                                  $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
156
    /**
157
     * Whether the model should throw a
158
     * ValidationException if it fails validation.
159
     *
160
     * @var bool
161
     */
162
    protected $throwValidationExceptions = true;
163
164
    /**
165
     * Create a new Eloquent model instance.
166
     *
167
     * @param array $attributes
168
     */
169
    public function __construct(array $attributes = [])
170
    {
171
        parent::__construct($attributes);
172
173
        $this->setTable(config('rinvex.contacts.tables.contacts'));
174
        $this->setRules([
175
            'entity_id' => 'required|integer',
176
            'entity_type' => 'required|string|max:150',
177
            'source' => 'required|string|max:150',
178
            'method' => 'nullable|string|max:150',
179
            'name_prefix' => 'nullable|string|max:150',
180
            'first_name' => 'required|string|max:150',
181
            'middle_name' => 'nullable|string|max:150',
182
            'last_name' => 'nullable|string|max:150',
183
            'name_suffix' => 'nullable|string|max:150',
184
            'job_title' => 'nullable|string|max:150',
185
            'email' => 'nullable|email|min:3|max:150',
186
            'phone' => 'nullable|numeric|min:4',
187
            'fax' => 'nullable|string|max:150',
188
            'skype' => 'nullable|string|max:150',
189
            'twitter' => 'nullable|string|max:150',
190
            'facebook' => 'nullable|string|max:150',
191
            'google_plus' => 'nullable|string|max:150',
192
            'linkedin' => 'nullable|string|max:150',
193
            'country_code' => 'nullable|alpha|size:2|country',
194
            'language_code' => 'nullable|alpha|size:2|language',
195
            'birthday' => 'nullable|date_format:Y-m-d',
196
            'gender' => 'nullable|string|in:m,f',
197
        ]);
198
    }
199
200
    /**
201
     * Get the contact name.
202
     *
203
     * @return string
204
     */
205
    public function getNameAttribute()
206
    {
207
        return trim(collect([
208
            $this->name_prefix,
209
            $this->first_name,
210
            $this->middle_name,
211
            $this->last_name,
212
            $this->name_suffix,
213
        ])->implode(' '));
214
    }
215
216
    /**
217
     * Get the owner model of the contact.
218
     *
219
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
220
     */
221
    public function entity(): MorphTo
222
    {
223
        return $this->morphTo();
224
    }
225
226
    /**
227
     * Enforce clean sources.
228
     *
229
     * @param string $value
230
     *
231
     * @return void
232
     */
233
    public function setSourceAttribute($value)
234
    {
235
        $this->attributes['source'] = str_slug($value);
236
    }
237
238
    /**
239
     * Enforce clean methods.
240
     *
241
     * @param string $value
242
     *
243
     * @return void
244
     */
245
    public function setMethodAttribute($value)
246
    {
247
        $this->attributes['method'] = str_slug($value);
248
    }
249
250
    /**
251
     * Scope contacts by the given country.
252
     *
253
     * @param \Illuminate\Database\Eloquent\Builder $builder
254
     * @param string                                $countryCode
255
     *
256
     * @return \Illuminate\Database\Eloquent\Builder
257
     */
258
    public function scopeCountry(Builder $builder, string $countryCode): Builder
259
    {
260
        return $builder->where('country_code', $countryCode);
261
    }
262
263
    /**
264
     * Scope contacts by the given language.
265
     *
266
     * @param \Illuminate\Database\Eloquent\Builder $builder
267
     * @param string                                $languageCode
268
     *
269
     * @return \Illuminate\Database\Eloquent\Builder
270
     */
271
    public function scopeLanguage(Builder $builder, string $languageCode): Builder
272
    {
273
        return $builder->where('language_code', $languageCode);
274
    }
275
276
    /**
277
     * Scope contacts by the given source.
278
     *
279
     * @param \Illuminate\Database\Eloquent\Builder $builder
280
     * @param string                                $source
281
     *
282
     * @return \Illuminate\Database\Eloquent\Builder
283
     */
284
    public function scopeSource(Builder $builder, string $source): Builder
285
    {
286
        return $builder->where('source', $source);
287
    }
288
289
    /**
290
     * Scope contacts by the given method.
291
     *
292
     * @param \Illuminate\Database\Eloquent\Builder $builder
293
     * @param string                                $method
294
     *
295
     * @return \Illuminate\Database\Eloquent\Builder
296
     */
297
    public function scopeMethod(Builder $builder, string $method): Builder
298
    {
299
        return $builder->where('method', $method);
300
    }
301
302
    /**
303
     * A contact may have multiple related contacts.
304
     *
305
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
306
     */
307
    public function relatives(): BelongsToMany
308
    {
309
        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...
310
                    ->withPivot('relation')->withTimestamps();
311
    }
312
313
    /**
314
     * A contact may be related to multiple contacts.
315
     *
316
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
317
     */
318
    public function backRelatives(): BelongsToMany
319
    {
320
        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...
321
                    ->withPivot('relation')->withTimestamps();
322
    }
323
}
324