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
|
|
|
/** @var float */ |
13
|
|
|
private $north; |
14
|
|
|
|
15
|
|
|
/** @var float */ |
16
|
|
|
private $east; |
17
|
|
|
|
18
|
|
|
/** @var float */ |
19
|
|
|
private $south; |
20
|
|
|
|
21
|
|
|
/** @var float */ |
22
|
|
|
private $west; |
23
|
|
|
|
24
|
|
|
private function __construct(float $north, float $east, float $south, float $west) |
25
|
|
|
{ |
26
|
|
|
$this->north = $north; |
27
|
|
|
$this->east = $east; |
28
|
|
|
$this->south = $south; |
29
|
|
|
$this->west = $west; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @psalm-pure |
34
|
|
|
* @psalm-mutation-free |
35
|
|
|
*/ |
36
|
|
|
public static function create(): self |
37
|
|
|
{ |
38
|
|
|
return new self(0, 0, 0, 0); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @psalm-mutation-free */ |
42
|
|
|
public function setNorth(float $north): self |
43
|
|
|
{ |
44
|
|
|
$this->validatePositive($north); |
45
|
|
|
|
46
|
|
|
return new self($north, $this->east, $this->south, $this->west); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @psalm-mutation-free */ |
50
|
|
|
public function setEast(float $east): self |
51
|
|
|
{ |
52
|
|
|
$this->validatePositive($east); |
53
|
|
|
|
54
|
|
|
return new self($this->north, $east, $this->south, $this->west); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @psalm-mutation-free */ |
58
|
|
|
public function setSouth(float $south): self |
59
|
|
|
{ |
60
|
|
|
$this->validatePositive($south); |
61
|
|
|
|
62
|
|
|
return new self($this->north, $this->east, $south, $this->west); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @psalm-mutation-free */ |
66
|
|
|
public function setWest(float $west): self |
67
|
|
|
{ |
68
|
|
|
$this->validatePositive($west); |
69
|
|
|
|
70
|
|
|
return new self($this->north, $this->east, $this->south, $west); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @psalm-mutation-free */ |
74
|
|
|
public function getNorth(): float |
75
|
|
|
{ |
76
|
|
|
return $this->north; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @psalm-mutation-free */ |
80
|
|
|
public function getEast(): float |
81
|
|
|
{ |
82
|
|
|
return $this->east; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @psalm-mutation-free */ |
86
|
|
|
public function getSouth(): float |
87
|
|
|
{ |
88
|
|
|
return $this->south; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @psalm-mutation-free */ |
92
|
|
|
public function getWest(): float |
93
|
|
|
{ |
94
|
|
|
return $this->west; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @psalm-pure |
99
|
|
|
* @psalm-mutation-free |
100
|
|
|
* |
101
|
|
|
* @throws InvalidDistanceException |
102
|
|
|
*/ |
103
|
|
|
private function validatePositive(float $value): void |
104
|
|
|
{ |
105
|
|
|
if ($value < 0) { |
106
|
|
|
throw new InvalidDistanceException('Negative distance is invalid.'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|