Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from JeroenDeDauw/cargo
Add cargo integration
- Loading branch information
Showing
26 changed files
with
648 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace Maps\Map\CargoFormat; | ||
|
||
use CargoDisplayFormat; | ||
use Maps\Map\CargoFormat\CargoOutputBuilder; | ||
use Maps\MapsFactory; | ||
|
||
class CargoFormat extends CargoDisplayFormat { | ||
|
||
private $parserOutput; | ||
|
||
public function __construct( \ParserOutput $parserOutput ) { | ||
parent::__construct( $parserOutput ); | ||
$this->parserOutput = $parserOutput; | ||
} | ||
|
||
public static function allowedParameters() { | ||
return [ | ||
'height' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-heightparam' )->parse() ], | ||
'width' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-widthparam' )->parse() ], | ||
'icon' => [ 'type' => 'string' ], | ||
'zoom' => [ 'type' => 'int' ] | ||
]; | ||
} | ||
|
||
public function display( array $valuesTable, array $formattedValuesTable, array $fieldDescriptions, array $displayParams ) { | ||
$mapOutput = $this->getResultBuilder()->buildOutputFromCargoData( | ||
$valuesTable, | ||
$formattedValuesTable, | ||
$fieldDescriptions, | ||
$displayParams | ||
); | ||
|
||
$this->parserOutput->addHeadItem( $mapOutput->getHeadItems() ); | ||
$this->parserOutput->addModules( $mapOutput->getResourceModules() ); | ||
|
||
return $mapOutput->getHtml(); | ||
} | ||
|
||
private function getResultBuilder(): CargoOutputBuilder { | ||
return MapsFactory::globalInstance()->newCargoOutputBuilder(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace Maps\Map\CargoFormat; | ||
|
||
use CargoFieldDescription; | ||
use DataValues\Geo\Values\LatLongValue; | ||
use Maps\DataAccess\ImageRepository; | ||
use Maps\Map\MapData; | ||
use Maps\Map\MapOutput; | ||
use Maps\Map\MapOutputBuilder; | ||
use Maps\Map\Marker; | ||
use Maps\Map\StructuredPopup; | ||
use Maps\MappingService; | ||
use Maps\MappingServices; | ||
use ParamProcessor\ParamDefinitionFactory; | ||
use ParamProcessor\Processor; | ||
|
||
class CargoOutputBuilder { | ||
|
||
private $outputBuilder; | ||
private $services; | ||
private $paramDefinitionFactory; | ||
private $imageRepository; | ||
|
||
public function __construct( MapOutputBuilder $outputBuilder, MappingServices $services, | ||
ParamDefinitionFactory $paramDefinitionFactory, ImageRepository $imageRepository ) { | ||
|
||
$this->outputBuilder = $outputBuilder; | ||
$this->services = $services; | ||
$this->paramDefinitionFactory = $paramDefinitionFactory; | ||
$this->imageRepository = $imageRepository; | ||
} | ||
|
||
public function buildOutputFromCargoData( array $values, array $formattedValues, array $fieldDescriptions, array $displayParams ): MapOutput { | ||
$service = $this->services->getDefaultService(); | ||
|
||
$mapData = $service->newMapDataFromProcessingResult( $this->processParameters( $service, $displayParams ) ); | ||
$mapData->setMarkers( $this->createMarkers( $formattedValues, $fieldDescriptions, $mapData ) ); | ||
|
||
return $this->outputBuilder->buildOutput( | ||
$service, | ||
$mapData | ||
); | ||
} | ||
|
||
private function createMarkers( array $formattedValues, array $fieldDescriptions, MapData $mapData ): array { | ||
$markers = $this->cargoValuesToMarkers( | ||
$formattedValues, | ||
$fieldDescriptions | ||
); | ||
|
||
$this->setIconUrl( $markers, $mapData->getParameters()['icon'] ); | ||
|
||
return $markers; | ||
} | ||
|
||
private function setIconUrl( array $markers, string $iconParameter ): void { | ||
if ( $iconParameter !== '' ) { | ||
$iconUrl = $this->imageRepository->getByName( $iconParameter )->getUrl(); | ||
|
||
foreach ( $markers as $marker ) { | ||
$marker->setIconUrl( $iconUrl ); | ||
} | ||
} | ||
} | ||
|
||
private function processParameters( MappingService $service, array $displayParams ) { | ||
$processor = Processor::newDefault(); | ||
|
||
$processor->setParameters( $displayParams ); | ||
$processor->setParameterDefinitions( $this->getParameterDefinitions( $service ) ); | ||
|
||
return $processor->processParameters(); | ||
} | ||
|
||
private function getParameterDefinitions( MappingService $service ): array { | ||
return $this->paramDefinitionFactory->newDefinitionsFromArrays( $service->getParameterInfo() ) ; | ||
} | ||
|
||
/** | ||
* @return Marker[] | ||
*/ | ||
private function cargoValuesToMarkers( array $formattedValues, array $fieldDescriptions ): array { | ||
$coordinateFields = $this->getCoordinateFieldNames( $fieldDescriptions ); | ||
$markers = []; | ||
|
||
foreach ( $formattedValues as $valuesRow ) { | ||
foreach ( $coordinateFields as $coordinateField ) { | ||
$markers[] = $this->newMarker( $valuesRow, $coordinateField, $coordinateFields ); | ||
} | ||
} | ||
|
||
return $markers; | ||
} | ||
|
||
/** | ||
* @param CargoFieldDescription[] $fieldDescriptions | ||
* @return string[] | ||
*/ | ||
private function getCoordinateFieldNames( array $fieldDescriptions ): array { | ||
$names = []; | ||
|
||
foreach ( $fieldDescriptions as $fieldName => $field ) { | ||
if ( $field->mType === 'Coordinates' ) { | ||
$names[] = str_replace( ' ', '_', $fieldName ); | ||
} | ||
} | ||
|
||
return $names; | ||
} | ||
|
||
private function newMarker( array $valuesRow, string $coordinateField, array $coordinateFields ): Marker { | ||
$marker = new Marker( new LatLongValue( | ||
(float)$valuesRow[$coordinateField . ' lat'], | ||
(float)$valuesRow[$coordinateField . ' lon'] | ||
) ); | ||
|
||
$marker->setText( | ||
( new StructuredPopup( | ||
array_shift( $valuesRow ) ?? '', | ||
$this->getPropertyValuesToDisplay( $valuesRow, $coordinateFields ) | ||
) )->getHtml() | ||
); | ||
|
||
return $marker; | ||
} | ||
|
||
private function getPropertyValuesToDisplay( array $valuesRow, array $coordinateFields ): array { | ||
$propertyValues = []; | ||
|
||
foreach ( $valuesRow as $name => $value ) { | ||
if ( $this->shouldDisplayValue( $name, $coordinateFields ) ) { | ||
$propertyValues[$name] = $value; | ||
} | ||
} | ||
|
||
return $propertyValues; | ||
} | ||
|
||
private function shouldDisplayValue( string $name, array $coordinateFields ): bool { | ||
$hiddenFields = []; | ||
|
||
foreach ( $coordinateFields as $field ) { | ||
$hiddenFields[] = $field; | ||
$hiddenFields[] = $field . ' lat'; | ||
$hiddenFields[] = $field . ' lon'; | ||
} | ||
|
||
return !in_array( $name, $hiddenFields ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.