1
|
|
|
<?php |
2
|
|
|
namespace SimpleCMS\Region\Casts; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\DB; |
5
|
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
6
|
|
|
|
7
|
|
|
class Point implements CastsAttributes |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Summary of select |
11
|
|
|
* @param mixed $field |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
|
|
public static function select($field): string |
15
|
|
|
{ |
16
|
|
|
return "ST_AsText($field) as $field"; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Transform the attribute from the underlying model values. |
21
|
|
|
* |
22
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
23
|
|
|
* @param string $key |
24
|
|
|
* @param mixed $value |
25
|
|
|
* @param array<string, mixed> $attributes |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
public function get($model, $key, $value, $attributes) |
29
|
|
|
{ |
30
|
|
|
if (empty($value)) { |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (!is_string($value) || !mb_check_encoding($value, 'UTF-8')) { |
35
|
|
|
$value = bin2hex($value); |
36
|
|
|
|
37
|
|
|
$value = unpack("x/x/x/x/corder/Ltype/dlat/dlon", pack("H*", $value)); |
38
|
|
|
|
39
|
|
|
return [(float) $value['lat'], (float) $value['lon']]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$pointString = str_replace(['POINT(', ')'], '', $value); |
43
|
|
|
$_value = array_reverse(explode(' ', trim($pointString))); |
44
|
|
|
|
45
|
|
|
return [(float) $_value[0], (float) $_value[1]]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Transform the attribute to its underlying model values. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
52
|
|
|
* @param string $key |
53
|
|
|
* @param mixed $value |
54
|
|
|
* @param array<string, mixed> $attributes |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function set($model, $key, $value, $attributes) |
58
|
|
|
{ |
59
|
|
|
$srid = 4326; |
60
|
|
|
return DB::raw("ST_GeomFromText('POINT($value[0] $value[1])',$srid)"); |
61
|
|
|
} |
62
|
|
|
} |