1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/value-object project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
|
13
|
|
|
final class GeoPoint implements ValueObjectInterface |
14
|
|
|
{ |
15
|
|
|
public const NULL_ISLAND = ['lon' => 0.0, 'lat' => 0.0]; |
16
|
|
|
|
17
|
|
|
private FloatValue $lon; |
18
|
|
|
|
19
|
|
|
private FloatValue $lat; |
20
|
|
|
|
21
|
|
|
/** @param float[] $point */ |
22
|
5 |
|
public static function fromArray(array $point): self |
23
|
|
|
{ |
24
|
5 |
|
Assertion::keyExists($point, 'lon'); |
25
|
5 |
|
Assertion::keyExists($point, 'lat'); |
26
|
5 |
|
return new self(FloatValue::fromNative($point['lon']), FloatValue::fromNative($point['lat'])); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** @param null|float[] $value */ |
30
|
5 |
|
public static function fromNative($value): self |
31
|
|
|
{ |
32
|
5 |
|
Assertion::nullOrIsArray($value, 'Trying to create GeoPoint VO from unsupported value type.'); |
33
|
5 |
|
return is_array($value) ? self::fromArray($value) : self::fromArray(self::NULL_ISLAND); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function toNative(): array |
37
|
|
|
{ |
38
|
2 |
|
return ['lon' => $this->lon->toNative(), 'lat' => $this->lat->toNative()]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @param self $comparator */ |
42
|
1 |
|
public function equals($comparator): bool |
43
|
|
|
{ |
44
|
1 |
|
Assertion::isInstanceOf($comparator, self::class); |
45
|
1 |
|
return $this->toNative() == $comparator->toNative(); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function __toString(): string |
49
|
|
|
{ |
50
|
1 |
|
return sprintf('lon: %s, lat: %s', (string)$this->lon, (string)$this->lat); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function isNullIsland(): bool |
54
|
|
|
{ |
55
|
|
|
return $this->toNative() == self::NULL_ISLAND; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function getLon(): FloatValue |
59
|
|
|
{ |
60
|
1 |
|
return $this->lon; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function getLat(): FloatValue |
64
|
|
|
{ |
65
|
1 |
|
return $this->lat; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
private function __construct(FloatValue $lon, FloatValue $lat) |
69
|
|
|
{ |
70
|
5 |
|
$this->lon = $lon; |
71
|
5 |
|
$this->lat = $lat; |
72
|
5 |
|
} |
73
|
|
|
} |
74
|
|
|
|