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 AbstractBuilder.
|
|
|
|
|
*
|
|
|
|
|
* @package Netzmacht\Contao\Leaflet\Builder
|
|
|
|
|
*/
|
|
|
|
|
abstract class AbstractMapper implements Mapper
|
|
|
|
|
{
|
2015-01-05 12:25:46 +01:00
|
|
|
const VALUE_NOT_EMPTY = '__value_not_empty__';
|
|
|
|
|
const VALUE_EMPTY = '__value_empty__';
|
|
|
|
|
|
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 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
|
|
|
|
|
*/
|
2015-01-05 12:25:46 +01:00
|
|
|
public function addConditionalOption($column, $option = null, $mapping = null, $value = self::VALUE_NOT_EMPTY)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
2015-01-05 12:25:46 +01:00
|
|
|
$option = $option ?: $column;
|
|
|
|
|
|
2014-12-29 12:17:40 +01:00
|
|
|
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
|
|
|
|
|
*/
|
2015-01-05 12:25:46 +01:00
|
|
|
public function addConditionalOptions($column, array $options, $value = self::VALUE_NOT_EMPTY)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
|
|
|
|
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}
|
|
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
public function handle(\Model $model, DefinitionMapper $builder, LatLngBounds $bounds = null)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
2015-01-06 14:55:53 +01:00
|
|
|
$definition = $this->createInstance($model, $builder, $bounds);
|
2014-12-29 12:17:40 +01:00
|
|
|
|
|
|
|
|
$this->buildOptions($definition, $model);
|
|
|
|
|
$this->buildConditionals($definition, $model);
|
2015-01-06 14:55:53 +01:00
|
|
|
$this->doBuild($definition, $model, $builder, $bounds);
|
2014-12-29 12:17:40 +01:00
|
|
|
|
|
|
|
|
return $definition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function match(\Model $model)
|
|
|
|
|
{
|
|
|
|
|
$modelClass = static::$modelClass;
|
|
|
|
|
|
|
|
|
|
return ($model instanceof $modelClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the builder.
|
|
|
|
|
*
|
|
|
|
|
* @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 $builder The definition builder.
|
|
|
|
|
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
2014-12-29 12:17:40 +01:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
protected function doBuild(
|
|
|
|
|
Definition $definition,
|
|
|
|
|
\Model $model,
|
|
|
|
|
DefinitionMapper $builder,
|
|
|
|
|
LatLngBounds $bounds = null
|
|
|
|
|
) {
|
2014-12-29 12:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new definition instance.
|
|
|
|
|
*
|
|
|
|
|
* @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.
|
2014-12-29 12:17:40 +01:00
|
|
|
*
|
|
|
|
|
* @return Definition
|
|
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
protected function createInstance(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
|
|
|
|
$reflector = new \ReflectionClass(static::$definitionClass);
|
2015-01-06 14:55:53 +01:00
|
|
|
$instance = $reflector->newInstanceArgs($this->buildConstructArguments($model, $mapper, $bounds));
|
2014-12-29 12:17:40 +01:00
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get construct arguments.
|
|
|
|
|
*
|
|
|
|
|
* @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.
|
2014-12-29 12:17:40 +01:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-01-06 14:55:53 +01:00
|
|
|
protected function buildConstructArguments(\Model $model, DefinitionMapper $mapper, LatLngBounds $bounds = null)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
|
|
|
|
return array(
|
2015-01-05 12:25:46 +01:00
|
|
|
$model->alias ?: (str_replace('tl_leaflet_', '', $model->getTable()) . '_' . $model->id)
|
2014-12-29 12:17:40 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2015-01-05 12:25:46 +01:00
|
|
|
if ($value === static::VALUE_EMPTY && empty($model->$column)) {
|
|
|
|
|
$this->applyOptions($options, $definition, $model);
|
|
|
|
|
} elseif ($value === static::VALUE_NOT_EMPTY && !empty($model->$column)) {
|
|
|
|
|
$this->applyOptions($options, $definition, $model);
|
|
|
|
|
} elseif ($model->$column == $value) {
|
2014-12-29 12:17:40 +01:00
|
|
|
$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.
|
|
|
|
|
*/
|
2014-12-29 16:19:43 +01:00
|
|
|
protected function applyOptions($options, $definition, $model)
|
2014-12-29 12:17:40 +01:00
|
|
|
{
|
|
|
|
|
foreach ($options as $option => $mapping) {
|
2014-12-29 16:19:43 +01:00
|
|
|
$setter = 'set' . ucfirst($option);
|
|
|
|
|
$default = $this->getDefaultOption($option, $definition);
|
2014-12-29 12:17:40 +01:00
|
|
|
|
2014-12-29 16:19:43 +01:00
|
|
|
if ($model->$mapping === '1' || $model->$mapping === '') {
|
|
|
|
|
if (((bool) $model->$option) !== $default) {
|
|
|
|
|
$definition->$setter($model->$mapping);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elseif ($model->$mapping !== $default) {
|
2014-12-29 12:17:40 +01:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|