mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-01 20:43:52 +01:00
Ongoing development.
This commit is contained in:
308
src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php
Normal file
308
src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php
Normal file
@@ -0,0 +1,308 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package dev
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2014 netzmacht creative David Molineus
|
||||
* @license LGPL 3.0
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
|
||||
/**
|
||||
* Class AbstractBuilder.
|
||||
*
|
||||
* @package Netzmacht\Contao\Leaflet\Builder
|
||||
*/
|
||||
abstract class AbstractMapper implements Mapper
|
||||
{
|
||||
/**
|
||||
* Class of the model being build.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $modelClass = null;
|
||||
|
||||
/**
|
||||
* Class of the definition being created.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $definitionClass = null;
|
||||
|
||||
/**
|
||||
* Options mapping.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $options = array();
|
||||
|
||||
/**
|
||||
* Conditional option mapping.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $conditional = array();
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a option mapping.
|
||||
*
|
||||
* @param string $option Name of the option.
|
||||
* @param string $mapping Mapping column name. Set if column name differs.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addOption($option, $mapping = null)
|
||||
{
|
||||
if (!isset($this->options[$option])) {
|
||||
$this->options[$option] = $this->getMapping($option, $mapping);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add options mapping.
|
||||
*
|
||||
* @param array|mixed $options List of option names.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addOptions($options)
|
||||
{
|
||||
$arguments = func_get_args();
|
||||
|
||||
if (count($arguments) > 1) {
|
||||
$options = $arguments;
|
||||
}
|
||||
|
||||
foreach ($options as $key => $value) {
|
||||
if (is_numeric($key)) {
|
||||
$this->addOption($value);
|
||||
} else {
|
||||
$this->addOption($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a conditional option.
|
||||
*
|
||||
* @param string $column Condition column.
|
||||
* @param string $option Option name.
|
||||
* @param null $mapping Mapping column name.
|
||||
* @param mixed $value Value of the conditional column.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addConditionalOption($column, $option, $mapping = null, $value = '1')
|
||||
{
|
||||
if (!isset($this->conditional[$column][$value][$option])) {
|
||||
$this->conditional[$column][$value][$option] = $this->getMapping($option, $mapping);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a conditional options.
|
||||
*
|
||||
* @param string $column Condition column.
|
||||
* @param array $options Option names.
|
||||
* @param mixed $value Value of the conditional column.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addConditionalOptions($column, array $options, $value = '1')
|
||||
{
|
||||
foreach ($options as $key => $option) {
|
||||
if (is_numeric($key)) {
|
||||
$this->addConditionalOption($column, $option, null, $value);
|
||||
} else {
|
||||
$this->addConditionalOption($column, $key, $option, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(\Model $model, DefinitionMapper $builder)
|
||||
{
|
||||
$definition = $this->createInstance($model, $builder);
|
||||
|
||||
$this->buildOptions($definition, $model);
|
||||
$this->buildConditionals($definition, $model);
|
||||
$this->doBuild($definition, $model, $builder);
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function match(\Model $model)
|
||||
{
|
||||
$modelClass = static::$modelClass;
|
||||
|
||||
return ($model instanceof $modelClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the builder.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function initialize();
|
||||
|
||||
/**
|
||||
* Use for specific build methods.
|
||||
*
|
||||
* @param Definition $definition The definition being built.
|
||||
* @param \Model $model The model.
|
||||
* @param DefinitionMapper $builder The definition builder.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function doBuild(Definition $definition, \Model $model, DefinitionMapper $builder)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new definition instance.
|
||||
*
|
||||
* @param \Model $model The model.
|
||||
* @param DefinitionMapper $mapper The definition mapper.
|
||||
*
|
||||
* @return Definition
|
||||
*/
|
||||
protected function createInstance(\Model $model, DefinitionMapper $mapper)
|
||||
{
|
||||
$reflector = new \ReflectionClass(static::$definitionClass);
|
||||
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper));
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get construct arguments.
|
||||
*
|
||||
* @param \Model $model The model.
|
||||
* @param DefinitionMapper $mapper The definition mapper.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
|
||||
{
|
||||
return array(
|
||||
$model->alias ?: $model->id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build options.
|
||||
*
|
||||
* @param Definition $definition The definition being built.
|
||||
* @param \Model $model The model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function buildOptions($definition, $model)
|
||||
{
|
||||
$this->applyOptions($this->options, $definition, $model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build conditional options.
|
||||
*
|
||||
* @param Definition $definition The definition being built.
|
||||
* @param \Model $model The model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function buildConditionals(Definition $definition, \Model $model)
|
||||
{
|
||||
foreach ($this->conditional as $column => $conditions) {
|
||||
foreach ($conditions as $value => $options) {
|
||||
if ($model->$column == $value) {
|
||||
$this->applyOptions($options, $definition, $model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mapping column.
|
||||
*
|
||||
* @param string $option Option name.
|
||||
* @param string|null $mapping Mapping column.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getMapping($option, $mapping)
|
||||
{
|
||||
if ($mapping === null) {
|
||||
return $option;
|
||||
}
|
||||
|
||||
return $mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply options from the model to the definition.
|
||||
*
|
||||
* @param array $options The options.
|
||||
* @param Definition $definition The definition being built.
|
||||
* @param \Model $model The model.
|
||||
*/
|
||||
private function applyOptions($options, $definition, $model)
|
||||
{
|
||||
foreach ($options as $option => $mapping) {
|
||||
$setter = 'set' . ucfirst($option);
|
||||
|
||||
if ($model->$option != $this->getDefaultOption($option, $definition)) {
|
||||
$definition->$setter($model->$mapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default option value.
|
||||
*
|
||||
* @param string $option The option name.
|
||||
* @param Definition $definition The definition being built.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function getDefaultOption($option, $definition)
|
||||
{
|
||||
$keys = array('has', 'is', 'get');
|
||||
$suffix = ucfirst($option);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$method = $key . $suffix;
|
||||
|
||||
if (method_exists($definition, $method)) {
|
||||
return $definition->$method();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
116
src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php
Normal file
116
src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package dev
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2014 netzmacht creative David Molineus
|
||||
* @license LGPL 3.0
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Event\BuildDefinitionEvent;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
|
||||
|
||||
/**
|
||||
* Class DefinitionBuilder is the main builder instance which contains all other builders as children.
|
||||
*
|
||||
* @package Netzmacht\Contao\Leaflet\Builder
|
||||
*/
|
||||
class DefinitionMapper
|
||||
{
|
||||
/**
|
||||
* Registered builders.
|
||||
*
|
||||
* @var AbstractMapper[][]
|
||||
*/
|
||||
private $builders = array();
|
||||
|
||||
/**
|
||||
* The event dispatcher.
|
||||
*
|
||||
* @var EventDispatcher
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
/**
|
||||
* Map id of the current built map.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $mapId;
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*
|
||||
* @param EventDispatcher $eventDispatcher The event dispatcher.
|
||||
*/
|
||||
public function __construct($eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a builder.
|
||||
*
|
||||
* @param Mapper $builder The builder.
|
||||
* @param int $priority The priority. The higher priorities get called first.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function register(Mapper $builder, $priority = 0)
|
||||
{
|
||||
$this->builders[$priority][] = $builder;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the map id of the current built map.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMapId()
|
||||
{
|
||||
return $this->mapId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a model.
|
||||
*
|
||||
* @param \Model $model The definition model.
|
||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||
*
|
||||
* @return Definition
|
||||
*/
|
||||
public function handle(\Model $model, $elementId = null)
|
||||
{
|
||||
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);
|
||||
|
||||
$event = new BuildDefinitionEvent($definition, $model);
|
||||
$this->eventDispatcher->dispatch($event::NAME, $event);
|
||||
|
||||
return $definition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException(
|
||||
sprintf(
|
||||
'Could not build model "%s::%s". No matching builders found.',
|
||||
$model->getTable(),
|
||||
$model->{$model->getPk()}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
106
src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php
Normal file
106
src/Netzmacht/Contao/Leaflet/Mapper/MapMapper.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package dev
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2014 netzmacht creative David Molineus
|
||||
* @license LGPL 3.0
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||
|
||||
use Netzmacht\Contao\Leaflet\Model\MapModel;
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
use Netzmacht\LeafletPHP\Definition\Map;
|
||||
|
||||
class MapMapper extends AbstractMapper
|
||||
{
|
||||
/**
|
||||
* Class of the model being build.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $modelClass = 'Netzmacht\Contao\Leaflet\Model\MapModel';
|
||||
|
||||
/**
|
||||
* Class of the definition being created.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $definitionClass = 'Netzmacht\LeafletPHP\Definition\Map';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function initialize()
|
||||
{
|
||||
$this
|
||||
->addOptions('center', 'zoom', 'zoomControl')
|
||||
->addOptions('dragging', 'touchZoom', 'scrollWheelZoom', 'doubleClickZoom', 'boxZoom', 'tap', 'keyboard')
|
||||
->addOptions('trackResize', 'closePopupOnClick', 'bounceAtZoomLimits')
|
||||
->addConditionalOptions('adjustZoomExtra', array('minZoom', 'maxZoom'))
|
||||
->addConditionalOptions('keyboard', array('keyboardPanOffset', 'keyboardZoomOffset'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function doBuild(Definition $map, \Model $model, DefinitionMapper $builder)
|
||||
{
|
||||
if ($map instanceof Map && $model instanceof MapModel) {
|
||||
$this->buildCustomOptions($map, $model);
|
||||
$this->buildControls($map, $model, $builder);
|
||||
$this->buildLayers($map, $model, $builder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper)
|
||||
{
|
||||
return array(
|
||||
$mapper->getMapId(),
|
||||
$mapper->getMapId()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build map custom options.
|
||||
*
|
||||
* @param Map $map The map being built.
|
||||
* @param MapModel $model The map model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function buildCustomOptions(Map $map, MapModel $model)
|
||||
{
|
||||
if ($model->options) {
|
||||
$map->setOptions(json_decode($model->options, true));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build map controls.
|
||||
*
|
||||
* @param Map $map The map being built.
|
||||
* @param MapModel $model The map model.
|
||||
* @param DefinitionMapper $mapper The definition mapper.
|
||||
*/
|
||||
private function buildControls(Map $map, MapModel$model, DefinitionMapper $mapper)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Build map layers.
|
||||
*
|
||||
* @param Map $map The map being built.
|
||||
* @param MapModel $model The map model.
|
||||
* @param DefinitionMapper $mapper Definition mapper.
|
||||
*/
|
||||
private function buildLayers(Map $map, MapModel $model, DefinitionMapper $mapper)
|
||||
{
|
||||
}
|
||||
}
|
||||
36
src/Netzmacht/Contao/Leaflet/Mapper/Mapper.php
Normal file
36
src/Netzmacht/Contao/Leaflet/Mapper/Mapper.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package dev
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @copyright 2014 netzmacht creative David Molineus
|
||||
* @license LGPL 3.0
|
||||
* @filesource
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Netzmacht\Contao\Leaflet\Mapper;
|
||||
|
||||
use Netzmacht\LeafletPHP\Definition;
|
||||
|
||||
interface Mapper
|
||||
{
|
||||
/**
|
||||
* Map model to the definition.
|
||||
*
|
||||
* @param \Model $model The model being built.
|
||||
* @param DefinitionMapper $builder The definition builder.
|
||||
*
|
||||
* @return Definition
|
||||
*/
|
||||
public function handle(\Model $model, DefinitionMapper $builder);
|
||||
|
||||
/**
|
||||
* Check if builder is responsible for the model.
|
||||
*
|
||||
* @param \Model $model The model being build.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function match(\Model $model);
|
||||
}
|
||||
Reference in New Issue
Block a user