1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Address\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Sfneal\Address\Builders\AddressBuilder; |
7
|
|
|
use Sfneal\Models\AbstractModel; |
8
|
|
|
use Sfneal\Models\Traits\CityStateAccessors; |
9
|
|
|
|
10
|
|
|
class Address extends AbstractModel |
11
|
|
|
{ |
12
|
|
|
// todo: create migration |
13
|
|
|
use CityStateAccessors; |
|
|
|
|
14
|
|
|
|
15
|
|
|
protected $connection = 'mysql'; |
16
|
|
|
protected $table = 'address'; |
17
|
|
|
protected $primaryKey = 'address_id'; |
18
|
|
|
|
19
|
|
|
protected $fillable = [ |
20
|
|
|
'address_id', |
21
|
|
|
'type', |
22
|
|
|
'address_1', |
23
|
|
|
'address_2', |
24
|
|
|
'city', |
25
|
|
|
'state', |
26
|
|
|
'zip', |
27
|
|
|
'addressable_id', |
28
|
|
|
'addressable_type', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Query Builder. |
33
|
|
|
* |
34
|
|
|
* @param $query |
35
|
|
|
* |
36
|
|
|
* @return AddressBuilder |
37
|
|
|
*/ |
38
|
|
|
public function newEloquentBuilder($query) |
39
|
|
|
{ |
40
|
|
|
return new AddressBuilder($query); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return AddressBuilder|Builder |
45
|
|
|
*/ |
46
|
|
|
public static function query(): AddressBuilder |
47
|
|
|
{ |
48
|
|
|
return parent::query(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the owning addressable model. |
53
|
|
|
*/ |
54
|
|
|
public function addressable() |
55
|
|
|
{ |
56
|
|
|
return $this->morphTo(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Retrieve an html block to display an address. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function show() |
65
|
|
|
{ |
66
|
|
|
$string = ''; |
67
|
|
|
if ($this->type) { |
|
|
|
|
68
|
|
|
$string .= '<small><b>'.ucfirst($this->type).' Address:</b></small><br>'; |
69
|
|
|
} |
70
|
|
|
if ($this->address_1) { |
|
|
|
|
71
|
|
|
$string .= "{$this->address_1}<br>"; |
72
|
|
|
} |
73
|
|
|
if ($this->address_2) { |
|
|
|
|
74
|
|
|
$string .= "{$this->address_2}<br>"; |
75
|
|
|
} |
76
|
|
|
if ($this->city) { |
|
|
|
|
77
|
|
|
$string .= "{$this->city_state_zip}<br>"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $string; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the 'address_1' attribute. |
85
|
|
|
* |
86
|
|
|
* @param $value |
87
|
|
|
*/ |
88
|
|
|
public function setAddress1Attribute($value) |
89
|
|
|
{ |
90
|
|
|
$this->attributes['address_1'] = (strlen(trim($value)) > 0) ? trim($value) : null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the 'address_2' attribute. |
95
|
|
|
* |
96
|
|
|
* @param $value |
97
|
|
|
*/ |
98
|
|
|
public function setAddress2Attribute($value) |
99
|
|
|
{ |
100
|
|
|
$this->attributes['address_2'] = (strlen(trim($value)) > 0) ? trim($value) : null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Parse city attribute into city and state values. |
105
|
|
|
* |
106
|
|
|
* @param $value |
107
|
|
|
*/ |
108
|
|
|
public function setCityAttribute($value) |
109
|
|
|
{ |
110
|
|
|
if (isset($value) && inString($value, ',')) { |
111
|
|
|
list($city, $state) = explode(',', $value); |
112
|
|
|
$this->attributes['city'] = ucfirst(trim($city)); |
113
|
|
|
$this->setStateAttribute($state); |
114
|
|
|
} else { |
115
|
|
|
$this->attributes['city'] = ucfirst(trim($value)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the 'state' attribute value. |
121
|
|
|
* |
122
|
|
|
* @param $value |
123
|
|
|
*/ |
124
|
|
|
public function setStateAttribute($value) |
125
|
|
|
{ |
126
|
|
|
// Check to see if a zip value was accidentally given |
127
|
|
|
if (arrayValuesEqual( |
128
|
|
|
collect(str_split($value)) |
129
|
|
|
->take(2) |
130
|
|
|
->map(function ($char) { |
131
|
|
|
return is_int($char); |
132
|
|
|
}) |
133
|
|
|
->toArray(), |
134
|
|
|
true |
135
|
|
|
)) { |
136
|
|
|
$attribute = 'zip'; |
137
|
|
|
} else { |
138
|
|
|
$attribute = 'state'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Only allow 2 chars, cast to uppercase, trim whitespace |
142
|
|
|
$this->attributes[$attribute] = (strlen(trim($value)) > 0) ? strtoupper(trim($value)) : null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set the 'zip' attribute. |
147
|
|
|
* |
148
|
|
|
* @param $value |
149
|
|
|
*/ |
150
|
|
|
public function setZipAttribute($value) |
151
|
|
|
{ |
152
|
|
|
$this->attributes['zip'] = (strlen(trim($value)) > 0) ? trim($value) : null; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|