Make definition mapper independent of the data format.

This commit is contained in:
David Molineus
2015-01-12 09:55:59 +01:00
parent ce715bbb0a
commit 9a37da12e6
4 changed files with 178 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
<?php
return array(
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber'
'Netzmacht\Contao\Leaflet\Subscriber\BootSubscriber',
'Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber',
);

View File

@@ -0,0 +1,82 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Event;
use Symfony\Component\EventDispatcher\Event;
/**
* Class GetHashEvent is emitted then a hash for a data object with an unknown type is required.
*
* @package Netzmacht\Contao\Leaflet\Event
*/
class GetHashEvent extends Event
{
const NAME = 'leaflet.get-hash';
/**
* The data.
*
* @var mixed
*/
private $data;
/**
* The hash.
*
* @var string
*/
private $hash;
/**
* Construct.
*
* @param mixed $data The data.
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Get the data.
*
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* Get the hash.
*
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* Set the generated hash.
*
* @param string $hash The generated hash.
*
* @return $this
*/
public function setHash($hash)
{
$this->hash = (string) $hash;
return $this;
}
}

View File

@@ -12,6 +12,7 @@
namespace Netzmacht\Contao\Leaflet\Mapper;
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
@@ -74,15 +75,15 @@ class DefinitionMapper
/**
* Build a model.
*
* @param \Model $model The definition model.
* @param mixed $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, LatLngBounds $bounds = null, $elementId = null)
public function handle($model, LatLngBounds $bounds = null, $elementId = null)
{
$hash = $model->getTable() . '.' . $model->{$model->getPk()} . ($elementId ? ('.' . $elementId) : '');
$hash = $this->getHash($model, $elementId);
if (isset($this->mapped[$hash])) {
return $this->mapped[$hash];
@@ -117,12 +118,12 @@ class DefinitionMapper
/**
* Build a model.
*
* @param \Model $model The definition model.
* @param mixed $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)
public function handleGeoJson($model, LatLngBounds $bounds = null)
{
foreach ($this->builders as $builders) {
foreach ($builders as $builder) {
@@ -152,4 +153,26 @@ class DefinitionMapper
)
);
}
/**
* @param $model
*
* @return string
*/
protected function getHash($model, $elementId)
{
$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;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Subscriber;
use Netzmacht\Contao\Leaflet\Event\GetHashEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HashSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
GetHashEvent::NAME => array(
array('getModelHash'),
array('getFallback', -100)
)
);
}
/**
* Get hash for a model object.
*
* @param GetHashEvent $event
*/
public function getModelHash(GetHashEvent $event)
{
$data = $event->getData();
if ($data instanceof \Model) {
$event->setHash($data->getTable() . '.' . $data->{$data->getPk()});
}
}
/**
* Get hash fallback if no hash was created so far.
*
* @param GetHashEvent $event
*/
public function getFallback(GetHashEvent $event)
{
if ($event->getHash()) {
return;
}
$data = $event->getData();
if (is_object($data)) {
$event->setHash(spl_object_hash($data));
} else {
$event->setHash(md5(json_encode($data)));
}
}
}