1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Location; |
5
|
|
|
|
6
|
|
|
use Location\Distance\DistanceInterface; |
7
|
|
|
use Location\Formatter\Polyline\FormatterInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Polyline Implementation |
11
|
|
|
* |
12
|
|
|
* @author Marcus Jaschen <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Polyline implements GeometryInterface |
15
|
|
|
{ |
16
|
|
|
use GetBoundsTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Coordinate[] |
20
|
|
|
*/ |
21
|
|
|
protected $points = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Coordinate $point |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function addPoint(Coordinate $point) |
29
|
|
|
{ |
30
|
|
|
$this->points[] = $point; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Coordinate[] |
35
|
|
|
*/ |
36
|
|
|
public function getPoints(): array |
37
|
|
|
{ |
38
|
|
|
return $this->points; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
public function getNumberOfPoints(): int |
45
|
|
|
{ |
46
|
|
|
return count($this->points); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param FormatterInterface $formatter |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function format(FormatterInterface $formatter): string |
55
|
|
|
{ |
56
|
|
|
return $formatter->format($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Line[] |
61
|
|
|
*/ |
62
|
|
|
public function getSegments(): array |
63
|
|
|
{ |
64
|
|
|
$length = count($this->points); |
65
|
|
|
$segments = []; |
66
|
|
|
|
67
|
|
|
if ($length <= 1) { |
68
|
|
|
return $segments; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
for ($i = 1; $i < $length; $i++) { |
|
|
|
|
72
|
|
|
$segments[] = new Line($this->points[$i - 1], $this->points[$i]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $segments; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Calculates the length of the polyline. |
80
|
|
|
* |
81
|
|
|
* @param DistanceInterface $calculator instance of distance calculation class |
82
|
|
|
* |
83
|
|
|
* @return float |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function getLength(DistanceInterface $calculator): float |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$distance = 0.0; |
88
|
|
|
|
89
|
|
|
if (count($this->points) <= 1) { |
90
|
|
|
return $distance; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($this->getSegments() as $segment) { |
94
|
|
|
$distance += $segment->getLength($calculator); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $distance; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create a new polyline with reversed order of points, i. e. reversed |
102
|
|
|
* polyline direction. |
103
|
|
|
* |
104
|
|
|
* @return Polyline |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function getReverse(): Polyline |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$reversed = new static(); |
109
|
|
|
|
110
|
|
|
foreach (array_reverse($this->points) as $point) { |
111
|
|
|
$reversed->addPoint($point); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $reversed; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.