1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leitom\Geo; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Redis; |
8
|
|
|
|
9
|
|
|
class Geo |
10
|
|
|
{ |
11
|
|
|
protected $app; |
12
|
|
|
protected $index; |
13
|
|
|
protected $config; |
14
|
|
|
protected $indexCache = []; |
15
|
|
|
protected $previousGeoSearch = []; |
16
|
|
|
|
17
|
|
|
public function __construct($app) |
18
|
|
|
{ |
19
|
|
|
$this->app = $app; |
20
|
|
|
$this->config = $app['config']->get('geo'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function index($index) |
24
|
|
|
{ |
25
|
|
|
$this->indexCache[] = $this->index = $index; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function search($longitude, $latitude, $radius, $sort = 'ASC') |
31
|
|
|
{ |
32
|
|
|
return $this->formatResults( |
33
|
|
|
Redis::georadius($this->resolveIndex(), $longitude, $latitude, $radius, $this->config['unit'], 'WITHDIST', $sort) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function between($locationA, $locationB) |
38
|
|
|
{ |
39
|
|
|
return Redis::geodist($this->resolveIndex(), $locationA->id, $locationB->id, $this->config['unit']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function from($location, $radius, $sort = 'ASC') |
43
|
|
|
{ |
44
|
|
|
return $this->formatResults( |
45
|
|
|
Redis::georadiusbymember($this->resolveIndex(), $location->id, $radius, $this->config['unit'], 'WITHDIST', $sort) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function add(...$locations) |
50
|
|
|
{ |
51
|
|
|
if (! Arr::first($locations) instanceof Coordinate) { |
|
|
|
|
52
|
|
|
$locations = Arr::collapse($locations); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
Redis::pipeline(function ($pipe) use ($locations) { |
56
|
|
|
foreach ($locations as $coordinate) { |
57
|
|
|
$pipe->geoadd($this->resolveIndex(), $coordinate->longitude, $coordinate->latitude, $coordinate->id); |
58
|
|
|
} |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function remove(...$ids) |
65
|
|
|
{ |
66
|
|
|
Redis::pipeline(function ($pipe) use ($ids) { |
67
|
|
|
foreach ($ids as $id) { |
68
|
|
|
$pipe->zrem($this->resolveIndex(), $id); |
69
|
|
|
} |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function clear($indexes = null) |
76
|
|
|
{ |
77
|
|
|
$indexes = $indexes ?? $this->indexCache; |
78
|
|
|
|
79
|
|
|
if (empty($indexes)) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$indexes = Collection::make($indexes)->map(function ($index) { |
84
|
|
|
return $this->resolveIndex($index); |
85
|
|
|
})->all(); |
86
|
|
|
|
87
|
|
|
Redis::del($indexes); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function previousGeoSearch($index = null) |
91
|
|
|
{ |
92
|
|
|
return Arr::get($this->previousGeoSearch, $this->resolveIndex($index ?? $this->index), []); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function formatResults($results) |
96
|
|
|
{ |
97
|
|
|
return $this->previousGeoSearch[$this->resolveIndex()] = Collection::make($results)->mapWithKeys(function ($result) { |
98
|
|
|
return [$result[0] => (float) $result[1]]; |
99
|
|
|
})->all(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function resolveIndex($index = null) |
103
|
|
|
{ |
104
|
|
|
$index = $index ?? $this->index; |
105
|
|
|
|
106
|
|
|
return $this->app->environment() === 'testing' ? "testing.{$index}" : $index; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: