mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2026-01-18 05:15:16 +01:00
Ongoing development.
This commit is contained in:
@@ -13,6 +13,9 @@ namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
|
||||
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
|
||||
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
|
||||
|
||||
/**
|
||||
@@ -70,6 +73,8 @@ class DefinitionMapper
|
||||
{
|
||||
$this->builders[$priority][] = $builder;
|
||||
|
||||
ksort($this->builders);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -86,12 +91,13 @@ class DefinitionMapper
|
||||
/**
|
||||
* Build a model.
|
||||
*
|
||||
* @param \Model $model The definition model.
|
||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||
* @param \Model $model The definition model.
|
||||
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||
*
|
||||
* @return Definition
|
||||
*/
|
||||
public function handle(\Model $model, $elementId = null)
|
||||
public function handle(\Model $model, LatLngBounds $bounds = null, $elementId = null)
|
||||
{
|
||||
$hash = $model->getTable() . '.' . $model->{$model->getPk()};
|
||||
|
||||
@@ -99,16 +105,14 @@ class DefinitionMapper
|
||||
return $this->mapped[$hash];
|
||||
}
|
||||
|
||||
krsort($this->builders);
|
||||
|
||||
$this->mapId = $elementId ?: ($model->alias ?: ('map_' . $model->id));
|
||||
|
||||
foreach ($this->builders as $builders) {
|
||||
foreach($builders as $builder) {
|
||||
if ($builder->match($model)) {
|
||||
$definition = $builder->handle($model, $this);
|
||||
$definition = $builder->handle($model, $this, $bounds);
|
||||
|
||||
$event = new BuildDefinitionEvent($definition, $model);
|
||||
$event = new BuildDefinitionEvent($definition, $model, $bounds);
|
||||
$this->eventDispatcher->dispatch($event::NAME, $event);
|
||||
|
||||
$this->mapped[$hash] = $definition;
|
||||
@@ -126,4 +130,43 @@ class DefinitionMapper
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a model.
|
||||
*
|
||||
* @param \Model $model The definition model.
|
||||
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
||||
*
|
||||
* @return FeatureCollection|Feature
|
||||
*/
|
||||
public function handleGeoJson(\Model $model, LatLngBounds $bounds = null)
|
||||
{
|
||||
foreach ($this->builders as $builders) {
|
||||
foreach ($builders as $builder) {
|
||||
if (!$builder->match($model)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($builder instanceof GeoJsonMapper) {
|
||||
return $builder->handleGeoJson($model, $this, $bounds);
|
||||
}
|
||||
|
||||
throw new \RuntimeException(
|
||||
sprintf(
|
||||
'Builder for model "%s::%s" is not a GeoJsonMapper',
|
||||
$model->getTable(),
|
||||
$model->{$model->getPk()}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException(
|
||||
sprintf(
|
||||
'Could not build geo json of model "%s::%s". No matching builders found.',
|
||||
$model->getTable(),
|
||||
$model->{$model->getPk()}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user