|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chuckcms\Contacts\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Chuckcms\Addresses\Traits\HasAddresses; |
|
6
|
|
|
use Chuckcms\Contacts\Contracts\Contact as ContactContract; |
|
7
|
|
|
use Chuckcms\Contacts\Exceptions\ContactDoesNotExist; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
10
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
11
|
|
|
|
|
12
|
|
|
class Contact extends Model implements ContactContract |
|
13
|
|
|
{ |
|
14
|
|
|
use HasAddresses, SoftDeletes; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
protected $guarded = ['id']; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The attributes that are fillable on this model. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $fillable = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The default rules that the model will validate against. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $rules = []; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(array $attributes = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->setTable(config('contacts.table_names.contacts')); |
|
|
|
|
|
|
35
|
|
|
$this->mergeFillables(); |
|
36
|
|
|
|
|
37
|
|
|
parent::__construct($attributes); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Merge fillable fields. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void. |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
|
|
private function mergeFillables() |
|
46
|
|
|
{ |
|
47
|
|
|
$fillable = $this->fillable; |
|
48
|
|
|
$columns = array_keys(config('contacts.fields.contacts')); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$this->fillable(array_merge($fillable, $columns)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the validation rules. |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getValidationRules(): array |
|
59
|
|
|
{ |
|
60
|
|
|
$rules = config('contacts.fields.contacts'); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $rules; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Scope public contacts. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
71
|
|
|
*/ |
|
72
|
|
|
public function scopeIsPublic(Builder $builder): Builder |
|
73
|
|
|
{ |
|
74
|
|
|
return $builder->where('is_public', true); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Scope primary contacts. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
83
|
|
|
*/ |
|
84
|
|
|
public function scopeIsPrimary(Builder $builder): Builder |
|
85
|
|
|
{ |
|
86
|
|
|
return $builder->where('is_primary', true); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public static function findById(int $id): ContactContract |
|
90
|
|
|
{ |
|
91
|
|
|
$contact = static::where('id', $id)->first(); |
|
92
|
|
|
|
|
93
|
|
|
if (!$contact) { |
|
94
|
|
|
throw ContactDoesNotExist::withId($id); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $contact; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|