Files
contao-leaflet-maps/src/Netzmacht/Contao/Leaflet/Mapper/AbstractMapper.php

196 lines
5.0 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +01:00
<?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;
2015-01-06 14:55:53 +01:00
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
2014-12-29 12:17:40 +01:00
/**
* Class AbstractMapper is made for mapping Contao models to the definition.
*
* For custom sources besides Contao models use your own implementation of the mapper interface.
2014-12-29 12:17:40 +01:00
*
* @package Netzmacht\Contao\Leaflet\Mapper
2014-12-29 12:17:40 +01:00
*/
abstract class AbstractMapper implements Mapper
{
2015-01-05 12:25:46 +01:00
const VALUE_NOT_EMPTY = '__value_not_empty__';
2015-01-12 19:03:29 +01:00
const VALUE_EMPTY = '__value_empty__';
2015-01-05 12:25:46 +01:00
2014-12-29 12:17:40 +01:00
/**
* 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 builder
2014-12-29 12:17:40 +01:00
*
* @var OptionsBuilder
2014-12-29 12:17:40 +01:00
*/
protected $optionsBuilder;
2014-12-29 12:17:40 +01:00
/**
* Construct.
*/
public function __construct()
{
$this->optionsBuilder = new OptionsBuilder();
2014-12-29 12:17:40 +01:00
$this->initialize();
}
/**
* {@inheritdoc}
*/
2015-01-20 16:38:23 +01:00
public function handle(
$model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null,
Definition $parent = null
) {
2015-01-09 13:41:09 +01:00
$definition = $this->createInstance($model, $mapper, $bounds, $elementId);
2014-12-29 12:17:40 +01:00
$this->optionsBuilder->build($definition, $model);
2015-01-20 16:38:23 +01:00
$this->build($definition, $model, $mapper, $bounds, $parent);
2014-12-29 12:17:40 +01:00
return $definition;
}
/**
* {@inheritdoc}
*/
public function match($model, LatLngBounds $bounds = null)
2014-12-29 12:17:40 +01:00
{
$modelClass = static::$modelClass;
return ($model instanceof $modelClass);
}
/**
* Initialize the mapper.
2014-12-29 12:17:40 +01:00
*
* @return void
*/
2014-12-29 16:19:43 +01:00
protected function initialize()
{
}
2014-12-29 12:17:40 +01:00
/**
* Use for specific build methods.
*
2015-01-06 14:55:53 +01:00
* @param Definition $definition The definition being built.
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
2015-01-06 14:55:53 +01:00
* @param LatLngBounds $bounds Optional bounds where elements should be in.
2015-01-20 16:38:23 +01:00
* @param Definition|null $parent The parent object.
2014-12-29 12:17:40 +01:00
*
* @return void
2015-01-12 19:03:29 +01:00
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2014-12-29 12:17:40 +01:00
*/
protected function build(
2015-01-06 14:55:53 +01:00
Definition $definition,
\Model $model,
DefinitionMapper $mapper,
2015-01-20 16:38:23 +01:00
LatLngBounds $bounds = null,
Definition $parent = null
2015-01-06 14:55:53 +01:00
) {
2014-12-29 12:17:40 +01:00
}
/**
* Create a new definition instance.
*
2015-01-09 13:41:09 +01:00
* @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.
2014-12-29 12:17:40 +01:00
*
* @return Definition
*/
2015-01-09 13:41:09 +01:00
protected function createInstance(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
$reflector = new \ReflectionClass($this->getClassName($model, $mapper, $bounds));
2015-01-09 13:41:09 +01:00
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds, $elementId));
2014-12-29 12:17:40 +01:00
return $instance;
}
/**
* Get construct arguments.
*
2015-01-12 19:03:29 +01:00
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
2015-01-09 13:41:09 +01:00
* @param string|null $elementId Optional element id.
2014-12-29 12:17:40 +01:00
*
* @return array
2015-01-12 19:03:29 +01:00
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2014-12-29 12:17:40 +01:00
*/
2015-01-09 13:41:09 +01:00
protected function buildConstructArguments(
\Model $model,
DefinitionMapper $mapper,
LatLngBounds $bounds = null,
$elementId = null
) {
2014-12-29 12:17:40 +01:00
return array(
2015-01-09 13:41:09 +01:00
$this->getElementId($model, $elementId)
2014-12-29 12:17:40 +01:00
);
}
/**
* Get definition class name.
*
* @param \Model $model The model.
* @param DefinitionMapper $mapper The definition mapper.
* @param LatLngBounds $bounds Optional bounds where elements should be in.
*
* @return string
2015-01-12 19:03:29 +01:00
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function getClassName(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
{
return static::$definitionClass;
}
2015-01-09 13:41:09 +01:00
/**
* 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);
}
2014-12-29 12:17:40 +01:00
}