1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daikon\Entity\ValueObject; |
4
|
|
|
|
5
|
|
|
use Daikon\Entity\Assert\Assertion; |
6
|
|
|
|
7
|
|
|
final class GeoPoint implements ValueObjectInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var float[] |
11
|
|
|
*/ |
12
|
|
|
public const NULL_ISLAND = [ |
13
|
|
|
"lon" => 0.0, |
14
|
|
|
"lat" => 0.0 |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Decimal |
19
|
|
|
*/ |
20
|
|
|
private $lon; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Decimal |
24
|
|
|
*/ |
25
|
|
|
private $lat; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param float[] $point |
29
|
|
|
* @return GeoPoint |
30
|
|
|
*/ |
31
|
22 |
|
public static function fromArray(array $point): self |
32
|
|
|
{ |
33
|
22 |
|
Assertion::keyExists($point, "lon"); |
34
|
22 |
|
Assertion::keyExists($point, "lat"); |
35
|
22 |
|
return new self(Decimal::fromNative($point['lon']), Decimal::fromNative($point['lat'])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param null|float[] $nativeValue |
40
|
|
|
* @return GeoPoint |
41
|
|
|
*/ |
42
|
22 |
|
public static function fromNative($nativeValue): self |
43
|
|
|
{ |
44
|
22 |
|
Assertion::nullOrIsArray($nativeValue); |
45
|
22 |
|
return is_array($nativeValue) ? self::fromArray($nativeValue) : self::fromArray(self::NULL_ISLAND); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return float[] |
50
|
|
|
*/ |
51
|
7 |
|
public function toNative(): array |
52
|
|
|
{ |
53
|
7 |
|
return [ "lon" => $this->lon->toNative(), "lat" => $this->lat->toNative() ]; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function equals(ValueObjectInterface $otherValue): bool |
57
|
|
|
{ |
58
|
1 |
|
return $otherValue instanceof self && $this->toNative() == $otherValue->toNative(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function __toString(): string |
62
|
|
|
{ |
63
|
1 |
|
return sprintf("lon: %s, lat: %s", $this->lon, $this->lat); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isNullIsland(): bool |
67
|
|
|
{ |
68
|
|
|
return $this->toNative() == self::NULL_ISLAND; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function getLon(): Decimal |
72
|
|
|
{ |
73
|
1 |
|
return $this->lon; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function getLat(): Decimal |
77
|
|
|
{ |
78
|
1 |
|
return $this->lat; |
79
|
|
|
} |
80
|
|
|
|
81
|
22 |
|
private function __construct(Decimal $lon, Decimal $lat) |
82
|
|
|
{ |
83
|
22 |
|
$this->lon = $lon; |
84
|
22 |
|
$this->lat = $lat; |
85
|
22 |
|
} |
86
|
|
|
} |
87
|
|
|
|