Address::query()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
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\Relations\MorphTo;
9
use Sfneal\Address\Builders\AddressBuilder;
10
use Sfneal\Address\Models\Traits\CityStateAccessors;
11
use Sfneal\Helpers\Arrays\ArrayHelpers;
12
use Sfneal\Helpers\Strings\StringHelpers;
13
use Sfneal\Models\Model;
14
15
class Address extends Model
16
{
17
    use CityStateAccessors;
18
    use HasFactory;
19
20
    protected $table = 'address';
21
    protected $primaryKey = 'address_id';
22
23
    protected $fillable = [
24
        'address_id',
25
        'type',
26
        'address_1',
27
        'address_2',
28
        'city',
29
        'state',
30
        'zip',
31
        'addressable_id',
32
        'addressable_type',
33
    ];
34
35
    protected $appends = [
36
        'address_full',
37
    ];
38
39
    /**
40
     * Model Factory.
41
     *
42
     * @return AddressFactory
43
     */
44
    protected static function newFactory(): AddressFactory
45
    {
46
        return new AddressFactory();
47
    }
48
49
    /**
50
     * Query Builder.
51
     *
52
     * @param  $query
53
     * @return AddressBuilder
54
     */
55
    public function newEloquentBuilder($query)
56
    {
57
        return new AddressBuilder($query);
58
    }
59
60
    /**
61
     * @return AddressBuilder|Builder
62
     */
63
    public static function query(): AddressBuilder
64
    {
65
        return parent::query();
66
    }
67
68
    /**
69
     * Get the owning addressable model.
70
     *
71
     * @return MorphTo
72
     */
73
    public function addressable(): MorphTo
74
    {
75
        return $this->morphTo();
76
    }
77
78
    /**
79
     * Retrieve an html block to display an address.
80
     *
81
     * @param  bool  $withType  include the address type
82
     * @return string
83
     */
84
    public function show(bool $withType = true): string
85
    {
86
        $string = '';
87
        if ($withType && $this->attributes['type']) {
88
            $string .= '<small><b>'.ucfirst($this->attributes['type']).' Address:</b></small><br>';
89
        }
90
        if ($this->attributes['address_1']) {
91
            $string .= "{$this->attributes['address_1']}<br>";
92
        }
93
        if ($this->attributes['address_2']) {
94
            $string .= "{$this->attributes['address_2']}<br>";
95
        }
96
        if ($this->attributes['city']) {
97
            $string .= "{$this->city_state_zip}<br>";
98
        }
99
100
        return $string;
101
    }
102
103
    /**
104
     * Retrieve the 'address_full' attribute.
105
     *
106
     *  - returns a full address string that includes address, city, state & zip
107
     *
108
     * @return string|null
109
     */
110
    public function getAddressFullAttribute(): ?string
111
    {
112
        if (! array_key_exists('address_1', $this->attributes)) {
113
            return null;
114
        }
115
116
        // Include the second line address if set
117
        $address = "{$this->attributes['address_1']}, ".(isset($this->attributes['address_2']) ? "{$this->attributes['address_2']}, " : '');
118
119
        return $address."{$this->attributes['city']}, {$this->attributes['state']} {$this->attributes['zip']}";
120
    }
121
122
    /**
123
     * Set the 'address_1' attribute.
124
     *
125
     * @param  $value
126
     * @return void
127
     */
128
    public function setAddress1Attribute($value): void
129
    {
130
        $this->attributes['address_1'] = (strlen(trim($value)) > 0) ? trim($value) : null;
131
    }
132
133
    /**
134
     * Set the 'address_2' attribute.
135
     *
136
     * @param  $value
137
     * @return void
138
     */
139
    public function setAddress2Attribute($value): void
140
    {
141
        $this->attributes['address_2'] = (strlen(trim($value)) > 0) ? trim($value) : null;
142
    }
143
144
    /**
145
     * Parse city attribute into city and state values.
146
     *
147
     * @param  $value
148
     * @return void
149
     */
150
    public function setCityAttribute($value): void
151
    {
152
        if (isset($value) && (new StringHelpers($value))->inString(',')) {
153
            [$city, $state] = explode(',', $value);
154
            $this->attributes['city'] = ucfirst(trim($city));
155
            $this->setStateAttribute(trim($state));
156
        } else {
157
            $this->attributes['city'] = ucfirst(trim($value));
158
        }
159
    }
160
161
    /**
162
     * Set the 'state' attribute value.
163
     *
164
     * @param  $value
165
     * @return void
166
     */
167
    public function setStateAttribute($value): void
168
    {
169
        // Check to see if a zip value was accidentally given
170
        if (ArrayHelpers::from(
171
            collect(str_split($value))
0 ignored issues
show
Bug introduced by
str_split($value) of type array|true is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
            collect(/** @scrutinizer ignore-type */ str_split($value))
Loading history...
172
                ->take(2)
173
                ->map(function ($char) {
174
                    return is_int($char);
175
                })
176
                ->toArray()
177
        )->valuesEqual(true)) {
178
            $attribute = 'zip';
179
        } else {
180
            $attribute = 'state';
181
        }
182
183
        // Only allow 2 chars, cast to uppercase, trim whitespace
184
        $this->attributes[$attribute] = (strlen(trim($value)) > 0) ? strtoupper(trim($value)) : null;
185
    }
186
187
    /**
188
     * Set the 'zip' attribute.
189
     *
190
     * @param  $value
191
     * @return void
192
     */
193
    public function setZipAttribute($value): void
194
    {
195
        $this->attributes['zip'] = (strlen(trim($value)) > 0) ? trim($value) : null;
196
    }
197
}
198