Extract geo json creation to an event subscriber.

This commit is contained in:
David Molineus
2015-01-23 13:04:04 +01:00
parent 0a5dc97f4b
commit b53887d78b
6 changed files with 266 additions and 40 deletions

View File

@@ -12,10 +12,13 @@
namespace Netzmacht\Contao\Leaflet\Mapper;
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJsonFeature;
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
@@ -132,6 +135,30 @@ class DefinitionMapper
);
}
/**
* Convert a definition to a geo json feature.
*
* @param Definition $definition The leaflet definition object.
* @param mixed $model The corresponding definition model.
*
* @return GeoJsonFeature
*/
public function convertToGeoJsonFeature(Definition $definition, $model)
{
if ($definition instanceof GeoJsonFeature) {
$feature = $definition;
} elseif ($definition instanceof ConvertsToGeoJsonFeature) {
$feature = $definition->toGeoJsonFeature();
} else {
throw new \RuntimeException('Unsupported definition');
}
$event = new ConvertToGeoJsonEvent($definition, $feature, $model);
$this->eventDispatcher->dispatch($event::NAME, $event);
return $feature;
}
/**
* Get the hash of a model.
*