1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Addresses\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
9
|
|
|
|
10
|
|
|
trait Addressable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Register a deleted model event with the dispatcher. |
14
|
|
|
* |
15
|
|
|
* @param \Closure|string $callback |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
abstract public static function deleted($callback); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Define a polymorphic one-to-many relationship. |
23
|
|
|
* |
24
|
|
|
* @param string $related |
25
|
|
|
* @param string $name |
26
|
|
|
* @param string $type |
|
|
|
|
27
|
|
|
* @param string $id |
|
|
|
|
28
|
|
|
* @param string $localKey |
|
|
|
|
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
31
|
|
|
*/ |
32
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Boot the addressable trait for the model. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public static function bootAddressable() |
40
|
|
|
{ |
41
|
|
|
static::deleted(function (self $model) { |
42
|
|
|
$model->addresses()->delete(); |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all attached addresses to the model. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
50
|
|
|
*/ |
51
|
|
|
public function addresses(): MorphMany |
52
|
|
|
{ |
53
|
|
|
return $this->morphMany(config('rinvex.addresses.models.address'), 'addressable'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Find addressables by distance. |
58
|
|
|
* |
59
|
|
|
* @param string $distance |
60
|
|
|
* @param string $unit |
61
|
|
|
* @param string $latitude |
62
|
|
|
* @param string $longitude |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Support\Collection |
65
|
|
|
*/ |
66
|
|
|
public static function findByDistance($distance, $unit, $latitude, $longitude): Collection |
67
|
|
|
{ |
68
|
|
|
$addressModel = config('rinvex.addresses.models.address'); |
69
|
|
|
$records = (new $addressModel())->within($distance, $unit, $latitude, $longitude)->get(); |
70
|
|
|
|
71
|
|
|
$results = []; |
72
|
|
|
foreach ($records as $record) { |
73
|
|
|
$results[] = $record->addressable; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new Collection($results); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.