Files
contao-leaflet-maps/src/Mapper/AbstractMapper.php

197 lines
5.0 KiB
PHP
Raw Normal View History

2014-12-29 12:17:40 +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
2014-12-29 12:17:40 +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
2014-12-29 12:17:40 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Mapper;
use Contao\Model;
2014-12-29 12:17:40 +01:00
use Netzmacht\LeafletPHP\Definition;
/**
* 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;
/**
2015-01-22 13:34:22 +01:00
* 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,
Request $request = null,
2015-01-20 16:38:23 +01:00
$elementId = null,
Definition $parent = null
) {
$definition = $this->createInstance($model, $mapper, $request, $elementId);
2014-12-29 12:17:40 +01:00
$this->optionsBuilder->build($definition, $model);
$this->build($definition, $model, $mapper, $request, $parent);
2014-12-29 12:17:40 +01:00
return $definition;
}
/**
* {@inheritdoc}
*/
public function match($model, Request $request = 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.
* @param Request $request Optional building request.
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,
Request $request = null,
2015-01-20 16:38:23 +01:00
Definition $parent = null
2015-01-06 14:55:53 +01:00
) {
2014-12-29 12:17:40 +01:00
}
/**
* Create a new definition instance.
*
* @param Model $model The model.
2015-01-09 13:41:09 +01:00
* @param DefinitionMapper $mapper The definition mapper.
* @param Request $request Optional building request.
2015-01-09 13:41:09 +01:00
* @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,
2015-01-09 13:41:09 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-09 13:41:09 +01:00
$elementId = null
) {
$reflector = new \ReflectionClass($this->getClassName($model, $mapper, $request));
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $request, $elementId));
2014-12-29 12:17:40 +01:00
return $instance;
}
/**
* Get construct arguments.
*
* @param Model $model The model.
2015-01-12 19:03:29 +01:00
* @param DefinitionMapper $mapper The definition mapper.
* @param Request $request Optional building request.
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,
2015-01-09 13:41:09 +01:00
DefinitionMapper $mapper,
Request $request = null,
2015-01-09 13:41:09 +01:00
$elementId = null
) {
return [
$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 Request $request Optional building request.
*
* @return string
2015-01-12 19:03:29 +01:00
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function getClassName(Model $model, DefinitionMapper $mapper, Request $request = 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.
2015-01-09 13:41:09 +01:00
* @param string|null $elementId Optional forced id.
*
* @return string
*/
protected function getElementId(Model $model, $elementId = null)
2015-01-09 13:41:09 +01:00
{
if ($elementId) {
return $elementId;
}
return $model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id);
}
2014-12-29 12:17:40 +01:00
}