1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
trait Geocoding |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Add one or more geospatial items to the specified key. This function must |
9
|
|
|
* be called with at least one longitude, latitude, member triplet. |
10
|
|
|
* |
11
|
|
|
* @param string $key |
12
|
|
|
* @param float $longitude |
13
|
|
|
* @param float $latitude |
14
|
|
|
* @param string $member |
15
|
|
|
* @param $locations |
16
|
|
|
* |
17
|
|
|
* @return int The number of elements added to the geospatial key. |
18
|
|
|
*/ |
19
|
|
|
public function geoAdd(string $key, float $longitude, float $latitude, string $member, ...$locations): int |
20
|
|
|
{ |
21
|
|
|
return $this->redis->geoAdd($key, $longitude, $latitude, $member, ...$locations); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Retrieve Geohash strings for one or more elements of a geospatial index. |
26
|
|
|
* |
27
|
|
|
* @param string $key |
28
|
|
|
* @param string $member |
29
|
|
|
* @param splat $members |
30
|
|
|
* |
31
|
|
|
* @return array One or more Redis Geohash encoded strings. |
32
|
|
|
*/ |
33
|
|
|
public function geoHash(string $key, string $member, ...$members): array |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
return $this->redis->geoHash($key, $member); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return longitude, latitude positions for each requested member. |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* @param string $member |
43
|
|
|
* @param splat $members |
44
|
|
|
* |
45
|
|
|
* @return array One or more longitude/latitude positions |
46
|
|
|
*/ |
47
|
|
|
public function geoPos(string $key, string $member, ...$members): array |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return $this->redis->geoPos($key, $member); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return the distance between two members in a geospatial set. If units |
54
|
|
|
* are passed it must be one of the following values:. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param string $member1 |
58
|
|
|
* @param string $member2 |
59
|
|
|
* @param string $unit |
60
|
|
|
* |
61
|
|
|
* @return float |
62
|
|
|
*/ |
63
|
|
|
public function geoDist(string $key, string $member1, string $member2, string $unit = 'm'): float |
64
|
|
|
{ |
65
|
|
|
return $this->redis->geoDist($key, $member1, $member2, $unit); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return members of a set with geospatial information that are within the |
70
|
|
|
* radius specified by the caller. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @param float $longitude |
74
|
|
|
* @param float $latitude |
75
|
|
|
* @param float $radius |
76
|
|
|
* @param string $unit |
77
|
|
|
* @param array $options |
78
|
|
|
* |
79
|
|
|
* @return array When no STORE option is passed, this function |
80
|
|
|
* returns an array of results. |
81
|
|
|
*/ |
82
|
|
|
public function geoRadius( |
83
|
|
|
string $key, |
84
|
|
|
float $longitude, |
85
|
|
|
float $latitude, |
86
|
|
|
float $radius, |
87
|
|
|
string $unit = 'm', |
88
|
|
|
?array $options = [] |
|
|
|
|
89
|
|
|
): array { |
90
|
|
|
return $this->redis->geoRadius($key, $longitude, $latitude, $radius, $unit); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* This method is identical to geoRadius except that instead of passing a |
95
|
|
|
* longitude and latitude as the "source" you pass an existing member in |
96
|
|
|
* the geospatial set. |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* @param string $member |
100
|
|
|
* @param float $radius |
101
|
|
|
* @param string $unit |
102
|
|
|
* @param array $options |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function geoRadiusByMember( |
107
|
|
|
string $key, |
108
|
|
|
string $member, |
109
|
|
|
float $radius, |
110
|
|
|
string $unit, |
111
|
|
|
?array $options = [] |
|
|
|
|
112
|
|
|
): array { |
113
|
|
|
return $this->redis->geoRadiusByMember($key, $member, $radius, $unit); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: