Files
contao-leaflet-maps/src/Mapper/DefinitionMapper.php

233 lines
6.3 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
2016-10-11 10:40:15 +02:00
* @package contao-leaflet-maps
2014-12-29 12:17:40 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
2017-10-11 15:00:48 +02:00
* @copyright 2014-2017 netzmacht David Molineus. All rights reserved.
2017-10-05 15:45:43 +02:00
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2014-12-29 12:17:40 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper;
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
2014-12-29 12:17:40 +01:00
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Value\GeoJson\ConvertsToGeoJsonFeature;
use Netzmacht\LeafletPHP\Value\GeoJson\Feature;
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Value\GeoJson\GeoJsonFeature;
2014-12-29 12:17:40 +01:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
/**
2015-01-22 10:01:30 +01:00
* Class DefinitionMapper is the main mapper instance which contains all other mappers as children.
2014-12-29 12:17:40 +01:00
*
2015-01-22 10:01:30 +01:00
* @package Netzmacht\Contao\Leaflet\Mapper
2014-12-29 12:17:40 +01:00
*/
class DefinitionMapper
{
/**
2015-01-12 19:03:29 +01:00
* Lit of all registered mappers.
2014-12-29 12:17:40 +01:00
*
2015-01-12 19:03:29 +01:00
* @var Mapper[][]
2014-12-29 12:17:40 +01:00
*/
private $mappers = [];
2014-12-29 12:17:40 +01:00
/**
* The event dispatcher.
*
* @var EventDispatcher
*/
private $eventDispatcher;
2015-01-05 14:58:37 +01:00
/**
2015-01-12 19:03:29 +01:00
* Cache of mapped definitions.
*
2015-01-05 14:58:37 +01:00
* @var array
*/
private $mapped = [];
2015-01-05 14:58:37 +01:00
2014-12-29 12:17:40 +01:00
/**
* Construct.
*
* @param EventDispatcher $eventDispatcher The event dispatcher.
*/
public function __construct($eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
2015-01-22 10:01:30 +01:00
* Add a mapper.
2014-12-29 12:17:40 +01:00
*
2015-01-22 13:34:22 +01:00
* @param Mapper $mapper The mapper.
2014-12-29 12:17:40 +01:00
* @param int $priority The priority. The higher priorities get called first.
*
* @return $this
*/
2015-01-22 10:01:30 +01:00
public function register(Mapper $mapper, $priority = 0)
2014-12-29 12:17:40 +01:00
{
2015-01-22 10:01:30 +01:00
$this->mappers[$priority][] = $mapper;
2014-12-29 12:17:40 +01:00
2015-01-22 10:01:30 +01:00
krsort($this->mappers);
2015-01-06 14:55:53 +01:00
2014-12-29 12:17:40 +01:00
return $this;
}
2015-01-28 16:40:48 +01:00
/**
* Reset the internal cache.
*
* @return $this
*/
public function reset()
{
$this->mapped = [];
2015-01-28 16:40:48 +01:00
return $this;
}
2014-12-29 12:17:40 +01:00
/**
* Build a model.
*
2015-01-20 16:38:23 +01:00
* @param mixed $model The definition model.
* @param Request|null $request The map request.
* @param string|null $elementId Optional element id. If none given the model id or alias is used.
2015-01-20 16:38:23 +01:00
* @param Definition|null $parent Optional pass the parent object.
2014-12-29 12:17:40 +01:00
*
2015-01-12 19:03:29 +01:00
* @return Definition|null
*
* @throws \RuntimeException If model could not be mapped to a definition.
2014-12-29 12:17:40 +01:00
*/
public function handle($model, Request $request = null, $elementId = null, $parent = null)
2014-12-29 12:17:40 +01:00
{
2015-01-22 10:01:30 +01:00
$hash = $this->hash($model, $elementId);
2015-01-05 14:58:37 +01:00
2015-01-22 10:01:30 +01:00
if (!isset($this->mapped[$hash])) {
$mapper = $this->getMapper($model);
$definition = $mapper->handle($model, $this, $request, $elementId, $parent);
2015-01-05 14:58:37 +01:00
2015-01-22 10:01:30 +01:00
if ($definition) {
$event = new BuildDefinitionEvent($definition, $model, $request);
2015-01-22 10:01:30 +01:00
$this->eventDispatcher->dispatch($event::NAME, $event);
2014-12-29 12:17:40 +01:00
}
2015-01-22 10:01:30 +01:00
$this->mapped[$hash] = $definition;
2014-12-29 12:17:40 +01:00
}
2015-01-22 10:01:30 +01:00
return $this->mapped[$hash];
2014-12-29 12:17:40 +01:00
}
2015-01-06 14:55:53 +01:00
/**
* Build a model.
*
* @param mixed $model The definition model.
* @param Request $request Optional building request.
2015-01-12 19:03:29 +01:00
*
* @return FeatureCollection|Feature|null
2015-01-06 14:55:53 +01:00
*
2015-01-12 19:03:29 +01:00
* @throws \RuntimeException If a model could not be mapped to the GeoJSON representation.
2015-01-06 14:55:53 +01:00
*/
public function handleGeoJson($model, Request $request = null)
2015-01-06 14:55:53 +01:00
{
2015-01-22 10:01:30 +01:00
$mapper = $this->getMapper($model);
2015-01-06 14:55:53 +01:00
2015-01-22 10:01:30 +01:00
if ($mapper instanceof GeoJsonMapper) {
return $mapper->handleGeoJson($model, $this, $request);
2015-01-06 14:55:53 +01:00
}
throw new \RuntimeException(
sprintf(
2015-01-22 10:01:30 +01:00
'Mapper for model "%s::%s" is not a GeoJsonMapper',
2015-01-06 14:55:53 +01:00
$model->getTable(),
$model->{$model->getPk()}
)
);
}
/**
* Convert a definition to a geo json feature.
*
* @param Definition $definition The leaflet definition object.
* @param mixed $model The corresponding definition model.
*
* @return GeoJsonFeature
2015-01-23 14:11:43 +01:00
* @throws \RuntimeException If a definition type is not supported.
*/
public function convertToGeoJsonFeature(Definition $definition, $model)
{
if ($definition instanceof GeoJsonFeature) {
$feature = $definition;
} elseif ($definition instanceof ConvertsToGeoJsonFeature) {
$feature = $definition->toGeoJsonFeature();
} else {
2015-01-23 14:11:43 +01:00
throw new \RuntimeException(
sprintf(
'Definition of class "%s" could not be converted to a geo json feature.',
get_class($definition)
)
);
}
$event = new ConvertToGeoJsonEvent($definition, $feature, $model);
$this->eventDispatcher->dispatch($event::NAME, $event);
return $feature;
}
/**
2015-01-12 19:03:29 +01:00
* Get the hash of a model.
*
* @param mixed $model The definition model.
* @param string|null $elementId Optional defined extra element id.
*
* @return string
2015-01-12 19:03:29 +01:00
*
* @throws \RuntimeException If no hash was created.
*/
2015-01-22 10:01:30 +01:00
private function hash($model, $elementId = null)
{
$event = new GetHashEvent($model);
$this->eventDispatcher->dispatch($event::NAME, $event);
$hash = $event->getHash();
if (!$hash) {
throw new \RuntimeException('Could not create a hash');
}
if ($elementId) {
$hash .= '.' . $elementId;
}
return $hash;
}
2015-01-22 10:01:30 +01:00
/**
* Get the mapper for a definition model.
*
* @param mixed $model The data model.
*
* @return Mapper
2015-01-22 13:34:22 +01:00
* @throws \RuntimeException If the mapper could not be found.
2015-01-22 10:01:30 +01:00
*/
private function getMapper($model)
{
foreach ($this->mappers as $mappers) {
foreach ($mappers as $mapper) {
if ($mapper->match($model)) {
return $mapper;
}
}
}
throw new \RuntimeException(
sprintf(
2015-01-22 13:34:22 +01:00
'Could not build model "%s". No matching mappers found.',
2015-01-22 10:01:30 +01:00
$this->hash($model)
)
);
}
2014-12-29 12:17:40 +01:00
}