Files
contao-leaflet-maps/src/Mapper/Layer/TileLayerMapper.php

109 lines
2.9 KiB
PHP
Raw Normal View History

2015-01-22 13:34:07 +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
2015-01-22 13:34:07 +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
2015-01-22 13:34:07 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper\Layer;
use Contao\Model;
use Contao\StringUtil;
2015-01-22 13:34:07 +01:00
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
2017-10-17 18:03:42 +02:00
use Netzmacht\Contao\Leaflet\Mapper\Request;
2015-01-22 13:34:07 +01:00
use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\Raster\TileLayer;
use Netzmacht\LeafletPHP\Value\LatLngBounds;
2015-01-22 13:34:07 +01:00
/**
* Class TileLayerMapper maps the database model to the tile layer definition.
*
* @package Netzmacht\Contao\Leaflet\Mapper\Layer
*/
class TileLayerMapper extends AbstractLayerMapper
{
/**
* The definition class.
*
* @var string
*/
protected static $definitionClass = TileLayer::class;
2015-01-22 13:34:07 +01:00
/**
* The layer type.
*
* @var string
*/
protected static $type = 'tile';
/**
* {@inheritdoc}
*/
protected function initialize()
{
parent::initialize();
$this->optionsBuilder
->addConditionalOption('minZoom')
->addConditionalOption('maxZoom')
->addConditionalOption('maxNativeZoom')
->addConditionalOption('tileSize')
->addConditionalOption('subdomain')
->addConditionalOption('errorTileUrl')
->addOptions('attribution', 'tms', 'continuousWorld', 'noWrap', 'zoomReverse')
->addConditionalOption('zoomOffset')
->addConditionalOption('opacity')
->addOption('zIndex')
->addOptions('unloadvisibleTiles', 'updateWhenIdle', 'detectRetina', 'reuseTiles');
}
/**
* {@inheritdoc}
*/
protected function buildConstructArguments(
Model $model,
2015-01-22 13:34:07 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-22 13:34:07 +01:00
$elementId = null
) {
$arguments = parent::buildConstructArguments($model, $mapper, $request, $elementId);
2015-01-22 13:34:07 +01:00
$arguments[] = $model->tileUrl;
return $arguments;
}
/**
* {@inheritdoc}
*/
protected function build(
Definition $definition,
Model $model,
2015-01-22 13:34:07 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-22 13:34:07 +01:00
Definition $parent = null
) {
parent::build($definition, $model, $mapper, $request, $parent);
2015-01-22 13:34:07 +01:00
/** @var TileLayer $definition */
$bounds = StringUtil::deserialize($model->bounds);
2015-01-22 13:34:07 +01:00
if ($request[0] && $request[1]) {
$bounds = array_map(
2015-01-22 13:34:07 +01:00
function ($value) {
return explode(',', $value, 3);
},
$bounds
2015-01-22 13:34:07 +01:00
);
$bounds = LatLngBounds::fromArray($bounds);
$definition->setBounds($bounds);
2015-01-22 13:34:07 +01:00
}
}
}