Test Failed
Push — master ( 65b1e6...cf15ae )
by Stephen
02:23
created

Address::setAddress1Attribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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