1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Maps\Map\CargoFormat; |
6
|
|
|
|
7
|
|
|
use CargoFieldDescription; |
8
|
|
|
use DataValues\Geo\Values\LatLongValue; |
9
|
|
|
use Maps\DataAccess\ImageRepository; |
10
|
|
|
use Maps\Map\MapData; |
11
|
|
|
use Maps\Map\MapOutput; |
12
|
|
|
use Maps\Map\MapOutputBuilder; |
13
|
|
|
use Maps\Map\Marker; |
14
|
|
|
use Maps\Map\StructuredPopup; |
15
|
|
|
use Maps\MappingService; |
16
|
|
|
use Maps\MappingServices; |
17
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
18
|
|
|
use ParamProcessor\Processor; |
19
|
|
|
|
20
|
|
|
class CargoOutputBuilder { |
21
|
|
|
|
22
|
|
|
private $outputBuilder; |
23
|
|
|
private $services; |
24
|
|
|
private $paramDefinitionFactory; |
25
|
|
|
private $imageRepository; |
26
|
|
|
|
27
|
|
|
public function __construct( MapOutputBuilder $outputBuilder, MappingServices $services, |
28
|
|
|
ParamDefinitionFactory $paramDefinitionFactory, ImageRepository $imageRepository ) { |
29
|
|
|
|
30
|
|
|
$this->outputBuilder = $outputBuilder; |
31
|
|
|
$this->services = $services; |
32
|
|
|
$this->paramDefinitionFactory = $paramDefinitionFactory; |
33
|
|
|
$this->imageRepository = $imageRepository; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function buildOutputFromCargoData( array $values, array $formattedValues, array $fieldDescriptions, array $displayParams ): MapOutput { |
|
|
|
|
37
|
|
|
$service = $this->services->getDefaultService(); |
38
|
|
|
|
39
|
|
|
$mapData = $service->newMapDataFromProcessingResult( $this->processParameters( $service, $displayParams ) ); |
40
|
|
|
$mapData->setMarkers( $this->createMarkers( $formattedValues, $fieldDescriptions, $mapData ) ); |
41
|
|
|
|
42
|
|
|
return $this->outputBuilder->buildOutput( |
43
|
|
|
$service, |
44
|
|
|
$mapData |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function createMarkers( array $formattedValues, array $fieldDescriptions, MapData $mapData ): array { |
49
|
|
|
$markers = $this->cargoValuesToMarkers( |
50
|
|
|
$formattedValues, |
51
|
|
|
$fieldDescriptions |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->setIconUrl( $markers, $mapData->getParameters()['icon'] ); |
55
|
|
|
|
56
|
|
|
return $markers; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function setIconUrl( array $markers, string $iconParameter ): void { |
60
|
|
|
if ( $iconParameter !== '' ) { |
61
|
|
|
$iconUrl = $this->imageRepository->getByName( $iconParameter )->getUrl(); |
62
|
|
|
|
63
|
|
|
foreach ( $markers as $marker ) { |
64
|
|
|
$marker->setIconUrl( $iconUrl ); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function processParameters( MappingService $service, array $displayParams ) { |
70
|
|
|
$processor = Processor::newDefault(); |
71
|
|
|
|
72
|
|
|
$processor->setParameters( $displayParams ); |
73
|
|
|
$processor->setParameterDefinitions( $this->getParameterDefinitions( $service ) ); |
74
|
|
|
|
75
|
|
|
return $processor->processParameters(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getParameterDefinitions( MappingService $service ): array { |
79
|
|
|
return $this->paramDefinitionFactory->newDefinitionsFromArrays( $service->getParameterInfo() ) ; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Marker[] |
84
|
|
|
*/ |
85
|
|
|
private function cargoValuesToMarkers( array $formattedValues, array $fieldDescriptions ): array { |
86
|
|
|
$coordinateFields = $this->getCoordinateFieldNames( $fieldDescriptions ); |
87
|
|
|
$markers = []; |
88
|
|
|
|
89
|
|
|
foreach ( $formattedValues as $valuesRow ) { |
90
|
|
|
foreach ( $coordinateFields as $coordinateField ) { |
91
|
|
|
$markers[] = $this->newMarker( $valuesRow, $coordinateField, $coordinateFields ); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $markers; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param CargoFieldDescription[] $fieldDescriptions |
100
|
|
|
* @return string[] |
101
|
|
|
*/ |
102
|
|
|
private function getCoordinateFieldNames( array $fieldDescriptions ): array { |
103
|
|
|
$names = []; |
104
|
|
|
|
105
|
|
|
foreach ( $fieldDescriptions as $fieldName => $field ) { |
106
|
|
|
if ( $field->mType === 'Coordinates' ) { |
107
|
|
|
$names[] = str_replace( ' ', '_', $fieldName ); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $names; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function newMarker( array $valuesRow, string $coordinateField, array $coordinateFields ): Marker { |
115
|
|
|
$marker = new Marker( new LatLongValue( |
116
|
|
|
(float)$valuesRow[$coordinateField . ' lat'], |
117
|
|
|
(float)$valuesRow[$coordinateField . ' lon'] |
118
|
|
|
) ); |
119
|
|
|
|
120
|
|
|
$marker->setText( |
121
|
|
|
( new StructuredPopup( |
122
|
|
|
array_shift( $valuesRow ) ?? '', |
123
|
|
|
$this->getPropertyValuesToDisplay( $valuesRow, $coordinateFields ) |
124
|
|
|
) )->getHtml() |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
return $marker; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function getPropertyValuesToDisplay( array $valuesRow, array $coordinateFields ): array { |
131
|
|
|
$propertyValues = []; |
132
|
|
|
|
133
|
|
|
foreach ( $valuesRow as $name => $value ) { |
134
|
|
|
if ( $this->shouldDisplayValue( $name, $coordinateFields ) ) { |
135
|
|
|
$propertyValues[$name] = $value; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $propertyValues; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function shouldDisplayValue( string $name, array $coordinateFields ): bool { |
143
|
|
|
$hiddenFields = []; |
144
|
|
|
|
145
|
|
|
foreach ( $coordinateFields as $field ) { |
146
|
|
|
$hiddenFields[] = $field; |
147
|
|
|
$hiddenFields[] = $field . ' lat'; |
148
|
|
|
$hiddenFields[] = $field . ' lon'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return !in_array( $name, $hiddenFields ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.