1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Location\CardinalDirection; |
6
|
|
|
|
7
|
|
|
use Location\Exception\InvalidDistanceException; |
8
|
|
|
|
9
|
|
|
/** @psalm-immutable */ |
10
|
|
|
class CardinalDirectionDistances |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var float |
14
|
|
|
*/ |
15
|
|
|
private $north; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var float |
19
|
|
|
*/ |
20
|
|
|
private $east; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var float |
24
|
|
|
*/ |
25
|
|
|
private $south; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var float |
29
|
|
|
*/ |
30
|
|
|
private $west; |
31
|
|
|
|
32
|
|
|
private function __construct(float $north, float $east, float $south, float $west) |
33
|
|
|
{ |
34
|
|
|
$this->north = $north; |
35
|
|
|
$this->east = $east; |
36
|
|
|
$this->south = $south; |
37
|
|
|
$this->west = $west; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @psalm-pure |
42
|
|
|
* @psalm-mutation-free |
43
|
|
|
*/ |
44
|
|
|
public static function create(): self |
45
|
|
|
{ |
46
|
|
|
return new self(0, 0, 0, 0); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @psalm-mutation-free |
51
|
|
|
*/ |
52
|
|
|
public function setNorth(float $north): self |
53
|
|
|
{ |
54
|
|
|
$this->assertPositiveFloat($north); |
55
|
|
|
|
56
|
|
|
return new self($north, $this->east, $this->south, $this->west); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @psalm-mutation-free |
61
|
|
|
*/ |
62
|
|
|
public function setEast(float $east): self |
63
|
|
|
{ |
64
|
|
|
$this->assertPositiveFloat($east); |
65
|
|
|
|
66
|
|
|
return new self($this->north, $east, $this->south, $this->west); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @psalm-mutation-free |
71
|
|
|
*/ |
72
|
|
|
public function setSouth(float $south): self |
73
|
|
|
{ |
74
|
|
|
$this->assertPositiveFloat($south); |
75
|
|
|
|
76
|
|
|
return new self($this->north, $this->east, $south, $this->west); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @psalm-mutation-free |
81
|
|
|
*/ |
82
|
|
|
public function setWest(float $west): self |
83
|
|
|
{ |
84
|
|
|
$this->assertPositiveFloat($west); |
85
|
|
|
|
86
|
|
|
return new self($this->north, $this->east, $this->south, $west); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @psalm-mutation-free |
91
|
|
|
*/ |
92
|
|
|
public function getNorth(): float |
93
|
|
|
{ |
94
|
|
|
return $this->north; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @psalm-mutation-free |
99
|
|
|
*/ |
100
|
|
|
public function getEast(): float |
101
|
|
|
{ |
102
|
|
|
return $this->east; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @psalm-mutation-free |
107
|
|
|
*/ |
108
|
|
|
public function getSouth(): float |
109
|
|
|
{ |
110
|
|
|
return $this->south; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @psalm-mutation-free |
115
|
|
|
*/ |
116
|
|
|
public function getWest(): float |
117
|
|
|
{ |
118
|
|
|
return $this->west; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @psalm-pure |
123
|
|
|
* @psalm-mutation-free |
124
|
|
|
* |
125
|
|
|
* @throws InvalidDistanceException |
126
|
|
|
*/ |
127
|
|
|
private function assertPositiveFloat(float $value): void |
128
|
|
|
{ |
129
|
|
|
if ($value < 0) { |
130
|
|
|
throw new InvalidDistanceException('Negative distance is invalid.', 1857757416); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|