This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Rinvex\Addresses\Models; |
||
6 | |||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Database\Eloquent\Builder; |
||
9 | use Rinvex\Addresses\Events\AddressSaved; |
||
10 | use Jackpopp\GeoDistance\GeoDistanceTrait; |
||
11 | use Rinvex\Support\Traits\ValidatingTrait; |
||
12 | use Rinvex\Addresses\Events\AddressDeleted; |
||
13 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
||
14 | |||
15 | /** |
||
16 | * Rinvex\Addresses\Models\Address. |
||
17 | * |
||
18 | * @property int $id |
||
19 | * @property int $addressable_id |
||
20 | * @property string $addressable_type |
||
21 | * @property string $label |
||
22 | * @property string $given_name |
||
23 | * @property string $family_name |
||
24 | * @property string $full_name |
||
25 | * @property string $organization |
||
26 | * @property string $country_code |
||
27 | * @property string $street |
||
28 | * @property string $state |
||
29 | * @property string $city |
||
30 | * @property string $postal_code |
||
31 | * @property float $latitude |
||
32 | * @property float $longitude |
||
33 | * @property bool $is_primary |
||
34 | * @property bool $is_billing |
||
35 | * @property bool $is_shipping |
||
36 | * @property \Carbon\Carbon|null $created_at |
||
37 | * @property \Carbon\Carbon|null $updated_at |
||
38 | * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $addressable |
||
39 | * |
||
40 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address inCountry($countryCode) |
||
41 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address inLanguage($languageCode) |
||
42 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address isBilling() |
||
43 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address isPrimary() |
||
44 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address isShipping() |
||
45 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address outside($distance, $measurement = null, $latitude = null, $longitude = null) |
||
46 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereAddressableId($value) |
||
47 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereAddressableType($value) |
||
48 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereCity($value) |
||
49 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereCountryCode($value) |
||
50 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereCreatedAt($value) |
||
51 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereFamilyName($value) |
||
52 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereGivenName($value) |
||
53 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereId($value) |
||
54 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereIsBilling($value) |
||
55 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereIsPrimary($value) |
||
56 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereIsShipping($value) |
||
57 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLabel($value) |
||
58 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLatitude($value) |
||
59 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLongitude($value) |
||
60 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereOrganization($value) |
||
61 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address wherePostalCode($value) |
||
62 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereState($value) |
||
63 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereStreet($value) |
||
64 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereUpdatedAt($value) |
||
65 | * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address within($distance, $measurement = null, $latitude = null, $longitude = null) |
||
66 | * @mixin \Eloquent |
||
67 | */ |
||
68 | class Address extends Model |
||
69 | { |
||
70 | use ValidatingTrait; |
||
71 | use GeoDistanceTrait; |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | protected $fillable = [ |
||
77 | 'addressable_id', |
||
78 | 'addressable_type', |
||
79 | 'label', |
||
80 | 'given_name', |
||
81 | 'family_name', |
||
82 | 'organization', |
||
83 | 'country_code', |
||
84 | 'street', |
||
85 | 'state', |
||
86 | 'city', |
||
87 | 'postal_code', |
||
88 | 'latitude', |
||
89 | 'longitude', |
||
90 | 'is_primary', |
||
91 | 'is_billing', |
||
92 | 'is_shipping', |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | protected $casts = [ |
||
99 | 'addressable_id' => 'integer', |
||
100 | 'addressable_type' => 'string', |
||
101 | 'label' => 'string', |
||
102 | 'given_name' => 'string', |
||
103 | 'family_name' => 'string', |
||
104 | 'organization' => 'string', |
||
105 | 'country_code' => 'string', |
||
106 | 'street' => 'string', |
||
107 | 'state' => 'string', |
||
108 | 'city' => 'string', |
||
109 | 'postal_code' => 'string', |
||
110 | 'latitude' => 'float', |
||
111 | 'longitude' => 'float', |
||
112 | 'is_primary' => 'boolean', |
||
113 | 'is_billing' => 'boolean', |
||
114 | 'is_shipping' => 'boolean', |
||
115 | 'deleted_at' => 'datetime', |
||
116 | ]; |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | protected $observables = [ |
||
122 | 'validating', |
||
123 | 'validated', |
||
124 | ]; |
||
125 | |||
126 | /** |
||
127 | * The event map for the model. |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | protected $dispatchesEvents = [ |
||
132 | 'saved' => AddressSaved::class, |
||
133 | 'deleted' => AddressDeleted::class, |
||
134 | ]; |
||
135 | |||
136 | /** |
||
137 | * The default rules that the model will validate against. |
||
138 | * |
||
139 | * @var array |
||
140 | */ |
||
141 | protected $rules = [ |
||
142 | 'addressable_id' => 'required|integer', |
||
143 | 'addressable_type' => 'required|string|strip_tags|max:150', |
||
144 | 'label' => 'nullable|string|strip_tags|max:150', |
||
145 | 'given_name' => 'required|string|strip_tags|max:150', |
||
146 | 'family_name' => 'nullable|string|strip_tags|max:150', |
||
147 | 'organization' => 'nullable|string|strip_tags|max:150', |
||
148 | 'country_code' => 'nullable|alpha|size:2|country', |
||
149 | 'street' => 'nullable|string|strip_tags|max:150', |
||
150 | 'state' => 'nullable|string|strip_tags|max:150', |
||
151 | 'city' => 'nullable|string|strip_tags|max:150', |
||
152 | 'postal_code' => 'nullable|string|strip_tags|max:150', |
||
153 | 'latitude' => 'nullable|numeric', |
||
154 | 'longitude' => 'nullable|numeric', |
||
155 | 'is_primary' => 'sometimes|boolean', |
||
156 | 'is_billing' => 'sometimes|boolean', |
||
157 | 'is_shipping' => 'sometimes|boolean', |
||
158 | ]; |
||
159 | |||
160 | /** |
||
161 | * Whether the model should throw a |
||
162 | * ValidationException if it fails validation. |
||
163 | * |
||
164 | * @var bool |
||
165 | */ |
||
166 | protected $throwValidationExceptions = true; |
||
167 | |||
168 | /** |
||
169 | * Create a new Eloquent model instance. |
||
170 | * |
||
171 | * @param array $attributes |
||
172 | */ |
||
173 | public function __construct(array $attributes = []) |
||
174 | { |
||
175 | parent::__construct($attributes); |
||
176 | |||
177 | $this->setTable(config('rinvex.addresses.tables.addresses')); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get the owner model of the address. |
||
182 | * |
||
183 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
184 | */ |
||
185 | public function addressable(): MorphTo |
||
186 | { |
||
187 | return $this->morphTo('addressable', 'addressable_type', 'addressable_id', 'id'); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Scope primary addresses. |
||
192 | * |
||
193 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
194 | * |
||
195 | * @return \Illuminate\Database\Eloquent\Builder |
||
196 | */ |
||
197 | public function scopeIsPrimary(Builder $builder): Builder |
||
198 | { |
||
199 | return $builder->where('is_primary', true); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Scope billing addresses. |
||
204 | * |
||
205 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
206 | * |
||
207 | * @return \Illuminate\Database\Eloquent\Builder |
||
208 | */ |
||
209 | public function scopeIsBilling(Builder $builder): Builder |
||
210 | { |
||
211 | return $builder->where('is_billing', true); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Scope shipping addresses. |
||
216 | * |
||
217 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
218 | * |
||
219 | * @return \Illuminate\Database\Eloquent\Builder |
||
220 | */ |
||
221 | public function scopeIsShipping(Builder $builder): Builder |
||
222 | { |
||
223 | return $builder->where('is_shipping', true); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Scope addresses by the given country. |
||
228 | * |
||
229 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
230 | * @param string $countryCode |
||
231 | * |
||
232 | * @return \Illuminate\Database\Eloquent\Builder |
||
233 | */ |
||
234 | public function scopeInCountry(Builder $builder, string $countryCode): Builder |
||
235 | { |
||
236 | return $builder->where('country_code', $countryCode); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Scope addresses by the given language. |
||
241 | * |
||
242 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
243 | * @param string $languageCode |
||
244 | * |
||
245 | * @return \Illuminate\Database\Eloquent\Builder |
||
246 | */ |
||
247 | public function scopeInLanguage(Builder $builder, string $languageCode): Builder |
||
248 | { |
||
249 | return $builder->where('language_code', $languageCode); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get full name attribute. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getFullNameAttribute(): string |
||
258 | { |
||
259 | return implode(' ', [$this->given_name, $this->family_name]); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | protected static function boot() |
||
266 | { |
||
267 | parent::boot(); |
||
268 | |||
269 | static::saving(function (self $address) { |
||
270 | if (config('rinvex.addresses.geocoding')) { |
||
271 | $segments[] = $address->street; |
||
0 ignored issues
–
show
|
|||
272 | $segments[] = sprintf('%s, %s %s', $address->city, $address->state, $address->postal_code); |
||
273 | $segments[] = country($address->country_code)->getName(); |
||
274 | |||
275 | $query = str_replace(' ', '+', implode(', ', $segments)); |
||
276 | $geocode = json_decode(file_get_contents("https://maps.google.com/maps/api/geocode/json?address={$query}&sensor=false")); |
||
277 | |||
278 | if (count($geocode->results)) { |
||
279 | $address->latitude = $geocode->results[0]->geometry->location->lat; |
||
280 | $address->longitude = $geocode->results[0]->geometry->location->lng; |
||
281 | } |
||
282 | } |
||
283 | }); |
||
284 | } |
||
285 | } |
||
286 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.