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\Map\MapOutput; |
10
|
|
|
use Maps\Map\MapOutputBuilder; |
11
|
|
|
use Maps\Map\Marker; |
12
|
|
|
use Maps\MappingService; |
13
|
|
|
use Maps\MappingServices; |
14
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
15
|
|
|
use ParamProcessor\Processor; |
16
|
|
|
|
17
|
|
|
class CargoOutputBuilder { |
18
|
|
|
|
19
|
|
|
private $outputBuilder; |
20
|
|
|
private $services; |
21
|
|
|
private $paramDefinitionFactory; |
22
|
|
|
|
23
|
|
|
public function __construct( MapOutputBuilder $outputBuilder, MappingServices $services, ParamDefinitionFactory $paramDefinitionFactory ) { |
24
|
|
|
$this->outputBuilder = $outputBuilder; |
25
|
|
|
$this->services = $services; |
26
|
|
|
$this->paramDefinitionFactory = $paramDefinitionFactory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function buildOutputFromCargoData( array $values, array $formattedValues, array $fieldDescriptions, array $displayParams ): MapOutput { |
|
|
|
|
30
|
|
|
$service = $this->services->getDefaultService(); |
31
|
|
|
|
32
|
|
|
$mapData = $service->newMapDataFromProcessingResult( $this->processParameters( $service, $displayParams ) ); |
33
|
|
|
|
34
|
|
|
$mapData->setMarkers( $this->cargoValuesToMarkers( |
35
|
|
|
$formattedValues, |
36
|
|
|
$fieldDescriptions |
37
|
|
|
) ); |
38
|
|
|
|
39
|
|
|
return $this->outputBuilder->buildOutput( |
40
|
|
|
$service, |
41
|
|
|
$mapData |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function processParameters( MappingService $service, array $displayParams ) { |
46
|
|
|
$processor = Processor::newDefault(); |
47
|
|
|
|
48
|
|
|
$processor->setParameters( $displayParams ); |
49
|
|
|
$processor->setParameterDefinitions( $this->getParameterDefinitions( $service ) ); |
50
|
|
|
|
51
|
|
|
return $processor->processParameters(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getParameterDefinitions( MappingService $service ): array { |
55
|
|
|
return $this->paramDefinitionFactory->newDefinitionsFromArrays( $service->getParameterInfo() ) ; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return Marker[] |
60
|
|
|
*/ |
61
|
|
|
private function cargoValuesToMarkers( array $formattedValues, array $fieldDescriptions ): array { |
62
|
|
|
$coordinateFields = $this->getCoordinateFieldNames( $fieldDescriptions ); |
63
|
|
|
$markers = []; |
64
|
|
|
|
65
|
|
|
foreach ( $formattedValues as $valuesRow ) { |
66
|
|
|
foreach ( $coordinateFields as $coordinateField ) { |
67
|
|
|
$markers[] = $this->newMarker( $valuesRow, $coordinateField, $coordinateFields ); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $markers; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param CargoFieldDescription[] $fieldDescriptions |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
|
|
private function getCoordinateFieldNames( array $fieldDescriptions ): array { |
79
|
|
|
$names = []; |
80
|
|
|
|
81
|
|
|
foreach ( $fieldDescriptions as $fieldName => $field ) { |
82
|
|
|
if ( $field->mType === 'Coordinates' ) { |
83
|
|
|
$names[] = str_replace( ' ', '_', $fieldName ); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $names; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function newMarker( array $valuesRow, string $coordinateField, array $coordinateFields ): Marker { |
91
|
|
|
$marker = new Marker( new LatLongValue( |
92
|
|
|
(float)$valuesRow[$coordinateField . ' lat'], |
93
|
|
|
(float)$valuesRow[$coordinateField . ' lon'] |
94
|
|
|
) ); |
95
|
|
|
|
96
|
|
|
$title = array_shift( $valuesRow ) ?? ''; |
97
|
|
|
$valueList = $this->getPropertyValueList( $valuesRow, $coordinateFields ); |
98
|
|
|
$separator = $title === '' || $valueList === '' ? '' : '<br><br>'; |
99
|
|
|
|
100
|
|
|
$marker->setText( '<strong>' . $title . '</strong>' . $separator . $valueList ); |
101
|
|
|
|
102
|
|
|
return $marker; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function getPropertyValueList( array $valuesRow, array $coordinateFields ): string { |
106
|
|
|
$lines = []; |
107
|
|
|
|
108
|
|
|
foreach ( $valuesRow as $name => $value ) { |
109
|
|
|
if ( $this->shouldDisplayValue( $name, $coordinateFields ) ) { |
110
|
|
|
$lines[] = $name . ': ' . $value; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return implode( '<br>', $lines ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function shouldDisplayValue( string $name, array $coordinateFields ): bool { |
118
|
|
|
$hiddenFields = []; |
119
|
|
|
|
120
|
|
|
foreach ( $coordinateFields as $field ) { |
121
|
|
|
$hiddenFields[] = $field; |
122
|
|
|
$hiddenFields[] = $field . ' lat'; |
123
|
|
|
$hiddenFields[] = $field . ' lon'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return !in_array( $name, $hiddenFields ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.