|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leitom\Geo; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Leitom\Geo\Facades\Geo; |
|
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Leitom\Geo\Events\ModelsRemoved; |
|
9
|
|
|
use Leitom\Geo\Events\ModelsImported; |
|
10
|
|
|
use Illuminate\Support\Collection as BaseCollection; |
|
11
|
|
|
|
|
12
|
|
|
trait HasGeoAbilities |
|
13
|
|
|
{ |
|
14
|
|
|
public $geoEnabled = true; |
|
15
|
|
|
|
|
16
|
|
|
public static function bootHasGeoAbilities() |
|
17
|
|
|
{ |
|
18
|
|
|
static::addGlobalScope(new GeoScope); |
|
19
|
|
|
|
|
20
|
|
|
static::observe(new ModelObserver); |
|
21
|
|
|
|
|
22
|
|
|
(new static)->registerGeoMacros(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function registerGeoMacros() |
|
26
|
|
|
{ |
|
27
|
|
|
$self = $this; |
|
28
|
|
|
|
|
29
|
|
|
BaseCollection::macro('addGeo', function () use ($self) { |
|
30
|
|
|
$self->addGeo($this); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
BaseCollection::macro('removeGeo', function () use ($self) { |
|
34
|
|
|
$self->removeGeo($this); |
|
35
|
|
|
}); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function addGeo($models) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($models->isEmpty()) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
Geo::index($models->first()->geoIndex())->add($models->map->toCoordinate()); |
|
45
|
|
|
|
|
46
|
|
|
event(new ModelsImported($models)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function removeGeo($models) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($models->isEmpty()) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
Geo::index($models->first()->geoIndex())->remove($models->map->geoKey()->all()); |
|
56
|
|
|
|
|
57
|
|
|
event(new ModelsRemoved($models)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public static function geoImportAll() |
|
61
|
|
|
{ |
|
62
|
|
|
$self = new static; |
|
63
|
|
|
|
|
64
|
|
|
$self->newQuery()->orderBy($self->geoKeyName())->addGeo(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function geoRemoveAll() |
|
68
|
|
|
{ |
|
69
|
|
|
$self = new static; |
|
70
|
|
|
|
|
71
|
|
|
$self->newQuery()->orderBy($self->geoKeyName())->removeGeo(); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function geoDistanceFrom($model) |
|
75
|
|
|
{ |
|
76
|
|
|
return [ |
|
77
|
|
|
'unit' => $this->geoUnit, |
|
|
|
|
|
|
78
|
|
|
'distance' => (float) Geo::between($this->toCoordinate(), $model->toCoordinate()), |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function scopeGeoNearest($builder, $radius = 10) |
|
83
|
|
|
{ |
|
84
|
|
|
$locations = array_keys(Geo::index($this->geoIndex())->from($this->toCoordinate(), $radius)); |
|
85
|
|
|
|
|
86
|
|
|
$builder->whereIn($this->geoKeyName(), $locations); |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
if ($this->databaseDriver() !== 'sqlite' && ! empty($locations)) { |
|
|
|
|
|
|
89
|
|
|
$builder->orderByRaw(DB::raw(sprintf('FIELD(%s,%s)', $this->geoKeyName(), implode(',', $locations)))); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function scopeGeoSearch($builder, $longitude, $latitude, $radius, $sort = 'ASC') |
|
94
|
|
|
{ |
|
95
|
|
|
$locations = array_keys(Geo::index($this->geoIndex())->search($longitude, $latitude, $radius, $sort)); |
|
96
|
|
|
|
|
97
|
|
|
$builder->whereIn($this->geoKeyName(), $locations); |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
if ($this->databaseDriver() !== 'sqlite' && ! empty($locations)) { |
|
|
|
|
|
|
100
|
|
|
$builder->orderByRaw(DB::raw(sprintf('FIELD(%s,%s)', $this->geoKeyName(), implode(',', $locations)))); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getGeoUnitAttribute() |
|
105
|
|
|
{ |
|
106
|
|
|
return config('geo.unit'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getGeoDistanceAttribute() |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->geoEnabled() && array_key_exists($this->geoKey(), $results = Geo::previousGeoSearch($this->geoIndex()))) { |
|
112
|
|
|
return Arr::get($results, $this->geoKey()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return 0; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function geoEnabled() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->geoEnabled; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function geoIndex() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->getTable(); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function geoKeyName() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getKeyName(); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function geoKey() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->getKey(); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function geoLongitude() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->longitude; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function geoLatitude() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->latitude; |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function toCoordinate() |
|
149
|
|
|
{ |
|
150
|
|
|
return new Coordinate($this->geoKey(), $this->geoLongitude(), $this->geoLatitude()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function toArray() |
|
154
|
|
|
{ |
|
155
|
|
|
$attributes = parent::toArray(); |
|
156
|
|
|
|
|
157
|
|
|
if ($this->geoEnabled()) { |
|
158
|
|
|
$attributes['geo_unit'] = $this->geoUnit; |
|
159
|
|
|
$attributes['geo_distance'] = $this->geoDistance; |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $attributes; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function databaseDriver() |
|
166
|
|
|
{ |
|
167
|
|
|
$connection = config('database.default'); |
|
168
|
|
|
|
|
169
|
|
|
return config("database.connections.{$connection}.driver"); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: