Passed
Push — master ( 191e78...c582c8 )
by Stephen
44s queued 11s
created

Address::getAddressFullAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Sfneal\Address\Models;
4
5
use Database\Factories\AddressFactory;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Model as EloquentModel;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
use Sfneal\Address\Builders\AddressBuilder;
11
use Sfneal\Helpers\Arrays\ArrayHelpers;
12
use Sfneal\Helpers\Strings\StringHelpers;
13
use Sfneal\Models\Model;
14
use Sfneal\Models\Traits\CityStateAccessors;
15
16
class Address extends Model
17
{
18
    use CityStateAccessors;
0 ignored issues
show
introduced by
The trait Sfneal\Models\Traits\CityStateAccessors requires some properties which are not provided by Sfneal\Address\Models\Address: $city, $state, $city_state, $zip
Loading history...
19
    use HasFactory;
20
21
    protected $table = 'address';
22
    protected $primaryKey = 'address_id';
23
24
    protected $fillable = [
25
        'address_id',
26
        'type',
27
        'address_1',
28
        'address_2',
29
        'city',
30
        'state',
31
        'zip',
32
        'addressable_id',
33
        'addressable_type',
34
    ];
35
36
    /**
37
     * Model Factory.
38
     *
39
     * @return AddressFactory
40
     */
41
    protected static function newFactory(): AddressFactory
42
    {
43
        return new AddressFactory();
44
    }
45
46
    /**
47
     * Query Builder.
48
     *
49
     * @param $query
50
     *
51
     * @return AddressBuilder
52
     */
53
    public function newEloquentBuilder($query)
54
    {
55
        return new AddressBuilder($query);
56
    }
57
58
    /**
59
     * @return AddressBuilder|Builder
60
     */
61
    public static function query(): AddressBuilder
62
    {
63
        return parent::query();
64
    }
65
66
    /**
67
     * Get the owning addressable model.
68
     *
69
     * @return MorphTo|Model|EloquentModel
70
     */
71
    public function addressable()
72
    {
73
        return $this->morphTo();
74
    }
75
76
    /**
77
     * Retrieve an html block to display an address.
78
     *
79
     * @param bool $withType include the address type
80
     * @return string
81
     */
82
    public function show(bool $withType = true): string
83
    {
84
        $string = '';
85
        if ($withType && $this->type) {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
86
            $string .= '<small><b>'.ucfirst($this->type).' Address:</b></small><br>';
87
        }
88
        if ($this->address_1) {
0 ignored issues
show
Bug introduced by
The property address_1 does not exist on Sfneal\Address\Models\Address. Did you mean addressable?
Loading history...
89
            $string .= "{$this->address_1}<br>";
90
        }
91
        if ($this->address_2) {
0 ignored issues
show
Bug introduced by
The property address_2 does not exist on Sfneal\Address\Models\Address. Did you mean addressable?
Loading history...
92
            $string .= "{$this->address_2}<br>";
93
        }
94
        if ($this->city) {
0 ignored issues
show
Bug introduced by
The property city does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
95
            $string .= "{$this->city_state_zip}<br>";
96
        }
97
98
        return $string;
99
    }
100
101
    /**
102
     * Retrieve the 'address_full' attribute.
103
     *
104
     *  - returns a full address string that includes address, city, state & zip
105
     *
106
     * @return string
107
     */
108
    public function getAddressFullAttribute(): string
109
    {
110
        // Include the second line address if set
111
        $address = "{$this->address_1}, ".(isset($this->address_2) ? "{$this->address_2}, " : '');
0 ignored issues
show
Bug introduced by
The property address_2 does not exist on Sfneal\Address\Models\Address. Did you mean addressable?
Loading history...
Bug introduced by
The property address_1 does not exist on Sfneal\Address\Models\Address. Did you mean addressable?
Loading history...
112
113
        return $address."{$this->city}, {$this->state} {$this->zip}";
0 ignored issues
show
Bug introduced by
The property city does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property state does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property zip does not seem to exist on Sfneal\Address\Models\Address. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
114
    }
115
116
    /**
117
     * Set the 'address_1' attribute.
118
     *
119
     * @param $value
120
     * @return void
121
     */
122
    public function setAddress1Attribute($value): void
123
    {
124
        $this->attributes['address_1'] = (strlen(trim($value)) > 0) ? trim($value) : null;
125
    }
126
127
    /**
128
     * Set the 'address_2' attribute.
129
     *
130
     * @param $value
131
     * @return void
132
     */
133
    public function setAddress2Attribute($value): void
134
    {
135
        $this->attributes['address_2'] = (strlen(trim($value)) > 0) ? trim($value) : null;
136
    }
137
138
    /**
139
     * Parse city attribute into city and state values.
140
     *
141
     * @param $value
142
     * @return void
143
     */
144
    public function setCityAttribute($value): void
145
    {
146
        if (isset($value) && (new StringHelpers($value))->inString(',')) {
147
            [$city, $state] = explode(',', $value);
148
            $this->attributes['city'] = ucfirst(trim($city));
149
            $this->setStateAttribute($state);
150
        } else {
151
            $this->attributes['city'] = ucfirst(trim($value));
152
        }
153
    }
154
155
    /**
156
     * Set the 'state' attribute value.
157
     *
158
     * @param $value
159
     * @return void
160
     */
161
    public function setStateAttribute($value): void
162
    {
163
        // Check to see if a zip value was accidentally given
164
        if ((new ArrayHelpers(
165
            collect(str_split($value))
166
                ->take(2)
167
                ->map(function ($char) {
168
                    return is_int($char);
169
                })
170
                ->toArray()
171
        ))->arrayValuesEqual(true)) {
172
            $attribute = 'zip';
173
        } else {
174
            $attribute = 'state';
175
        }
176
177
        // Only allow 2 chars, cast to uppercase, trim whitespace
178
        $this->attributes[$attribute] = (strlen(trim($value)) > 0) ? strtoupper(trim($value)) : null;
179
    }
180
181
    /**
182
     * Set the 'zip' attribute.
183
     *
184
     * @param $value
185
     * @return void
186
     */
187
    public function setZipAttribute($value): void
188
    {
189
        $this->attributes['zip'] = (strlen(trim($value)) > 0) ? trim($value) : null;
190
    }
191
}
192