Rework element id generation.

This commit is contained in:
David Molineus
2015-01-09 13:41:09 +01:00
parent 37851110b0
commit efb36256c7
12 changed files with 128 additions and 88 deletions

View File

@@ -151,9 +151,9 @@ abstract class AbstractMapper implements Mapper
/**
* {@inheritdoc}
*/
public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null)
{
$definition = $this->createInstance($model, $mapper, $bounds);
$definition = $this->createInstance($model, $mapper, $bounds, $elementId);
$this->buildOptions($definition, $model);
$this->buildConditionals($definition, $model);
@@ -202,16 +202,21 @@ abstract class AbstractMapper implements Mapper
/**
* Create a new definition instance.
*
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param string|null $elementId Optional element id.
*
* @return Definition
*/
protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
protected function createInstance(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$reflector = new \ReflectionClass($this->getClassName($model, $mapper, $bounds));
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds));
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds, $elementId));
return $instance;
}
@@ -222,13 +227,18 @@ abstract class AbstractMapper implements Mapper
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param string|null $elementId Optional element id.
*
* @return array
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
return array(
$model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id)
$this->getElementId($model, $elementId)
);
}
@@ -346,4 +356,21 @@ abstract class AbstractMapper implements Mapper
{
return static::$definitionClass;
}
/**
* Create element id for the model.
*
* @param \Model $model The model being passed.
* @param string|null $elementId Optional forced id.
*
* @return string
*/
protected function getElementId(\Model $model, $elementId = null)
{
if ($elementId) {
return $elementId;
}
return $model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id);
}
}

View File

@@ -50,8 +50,12 @@ class AttributionControlMapper extends AbstractControlMapper
/**
* {@inheritdoc}
*/
protected function build(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
{
protected function build(
Definition $definition,
\Model $model,
DefinitionMapper $builder,
LatLngBounds $bounds = null
) {
if (!$definition instanceof Attribution) {
return;
}

View File

@@ -32,9 +32,16 @@ class LayersControlMapper extends AbstractControlMapper
*/
protected static $type = 'layers';
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
$arguments[1] = array();
$arguments[2] = array();

View File

@@ -39,13 +39,6 @@ class DefinitionMapper
*/
private $eventDispatcher;
/**
* Map id of the current built map.
*
* @var string
*/
private $mapId;
/**
* @var array
*/
@@ -73,21 +66,11 @@ class DefinitionMapper
{
$this->builders[$priority][] = $builder;
ksort($this->builders);
krsort($this->builders);
return $this;
}
/**
* Get the map id of the current built map.
*
* @return string
*/
public function getMapId()
{
return $this->mapId;
}
/**
* Build a model.
*
@@ -105,15 +88,15 @@ class DefinitionMapper
return $this->mapped[$hash];
}
$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, $bounds);
$definition = $builder->handle($model, $this, $bounds, $elementId);
$event = new BuildDefinitionEvent($definition, $model, $bounds);
$this->eventDispatcher->dispatch($event::NAME, $event);
if ($definition) {
$event = new BuildDefinitionEvent($definition, $model, $bounds);
$this->eventDispatcher->dispatch($event::NAME, $event);
}
$this->mapped[$hash] = $definition;

View File

@@ -68,9 +68,11 @@ class GroupLayerMapper extends AbstractLayerMapper
if ($collection) {
foreach ($collection as $layerModel) {
/** @var Layer $layer */
$layer = $mapper->handle($layerModel);
$definition->addLayer($layer);
if ($layer instanceof Layer) {
$definition->addLayer($layer);
}
}
}
}

View File

@@ -65,8 +65,12 @@ class ProviderLayerMapper extends AbstractLayerMapper
/**
* {@inheritdoc}
*/
protected function build(Definition $definition, \Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
{
protected function build(
Definition $definition,
\Model $model,
DefinitionMapper $builder,
LatLngBounds $bounds = null
) {
if (!empty($this->providers[$model->tile_provider]['options'])) {
$this->applyOptions(
$this->providers[$model->tile_provider]['options'],
@@ -79,8 +83,12 @@ class ProviderLayerMapper extends AbstractLayerMapper
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
return array(
$model->alias ?: ('layer_' . $model->id),
$model->tile_provider,

View File

@@ -15,9 +15,11 @@ use Netzmacht\Contao\Leaflet\Model\ControlModel;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Control;
use Netzmacht\LeafletPHP\Definition\Layer;
use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Netzmacht\LeafletPHP\Plugins\LeafletProviders\Provider;
class MapMapper extends AbstractMapper
{
@@ -63,11 +65,15 @@ class MapMapper extends AbstractMapper
/**
* @inheritdoc
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
return array(
$mapper->getMapId(),
$mapper->getMapId()
$this->getElementId($model, $elementId),
$this->getElementId($model, $elementId)
);
}
@@ -96,15 +102,16 @@ class MapMapper extends AbstractMapper
*/
private function buildControls(Map $map, MapModel $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$collection = ControlModel::findBy(
array('pid=?', 'active=1'),
array($model->id),
array('order' => 'sorting')
);
$collection = ControlModel::findActiveBy('pid', $model->id, array('order' => 'sorting'));
if ($collection) {
foreach ($collection as $control) {
$control = $mapper->handle($control, $bounds);
if (!$collection) {
return;
}
foreach ($collection as $control) {
$control = $mapper->handle($control, $bounds);
if ($control instanceof Control) {
$map->addControl($control);
}
}
@@ -130,9 +137,9 @@ class MapMapper extends AbstractMapper
}
$layer = $mapper->handle($layer, $bounds);
/** @var Provider $layer */
$map->addLayer($layer);
if ($layer instanceof Layer) {
$map->addLayer($layer);
}
}
}
}

View File

@@ -19,13 +19,14 @@ interface Mapper
/**
* Map model to the definition.
*
* @param \Model|mixed $model The model being built. Usually a contao model. but can be anything
* @param DefinitionMapper $mapper The definition builder.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param \Model|mixed $model The model being built. Usually a contao model. but can be anything
* @param DefinitionMapper $mapper The definition builder.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
* @param string $elementId Optional element id.
*
* @return Definition
*/
public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null);
public function handle($model, DefinitionMapper $mapper, LatLngBounds $bounds = null, $elementId = null);
/**
* Check if mapper is responsible for the model.

View File

@@ -37,9 +37,13 @@ class ImageIconMapper extends AbstractIconMapper
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
if ($model->iconImage) {
$file = \FilesModel::findByUuid($model->iconImage);

View File

@@ -39,9 +39,13 @@ class MarkerMapper extends AbstractMapper
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
$arguments[] = $model->coordinates ?: null;
return $arguments;

View File

@@ -30,20 +30,9 @@ class AbstractVectorMapper extends AbstractTypeMapper
*/
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\VectorModel';
protected function initialize()
{
parent::initialize();
// $this
// ->addOptions('stroke', 'weight', 'opacity', 'clickable', 'className')
// ->addConditionalOption('color')
// ->addConditionalOption('lineCap')
// ->addConditionalOption('lineJoin')
// ->addConditionalOption('dashArray')
// ->addConditionalOptions('fill', array('fill', 'fillColor', 'fillOpacity'))
// ;
}
/**
* {@inheritdoc}
*/
protected function build(
Definition $definition,
\Model $model,

View File

@@ -40,8 +40,12 @@ class RectangleMapper extends AbstractVectorMapper
parent::initialize();
}
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$latLngs = array_map(
function($latLng) {
return LatLng::fromString($latLng);
@@ -49,7 +53,7 @@ class RectangleMapper extends AbstractVectorMapper
deserialize($model->bounds, true)
);
$arguments = parent::buildConstructArguments($model, $mapper, $bounds);
$arguments = parent::buildConstructArguments($model, $mapper, $bounds, $elementId);
$arguments[] = new LatLngBounds($latLngs[0], $latLngs[1]);
return $arguments;