Rework filter handling (Fixes #50).

This commit is contained in:
David Molineus
2017-10-11 14:40:01 +02:00
parent e1685f65c5
commit 5ce9d517bc
7 changed files with 92 additions and 55 deletions

View File

@@ -46,7 +46,6 @@ class NetzmachtContaoLeafletExtension extends Extension
$loader->load('providers.yml'); $loader->load('providers.yml');
// Other services // Other services
$loader->load('filters.yml');
$loader->load('mappers.yml'); $loader->load('mappers.yml');
$loader->load('encoders.yml'); $loader->load('encoders.yml');
$loader->load('layers.yml'); $loader->load('layers.yml');

View File

@@ -58,3 +58,8 @@ parameters:
- 'id' - 'id'
- 'title' - 'title'
- 'alias' - 'alias'
# Filters can be passed to a data request to get only specific data from a layer.
netzmacht.contao_leaflet.filters:
bbox: Netzmacht\Contao\Leaflet\Filter\BboxFilter
distance: Netzmacht\Contao\Leaflet\Filter\DistanceFilter

View File

@@ -1,11 +0,0 @@
services:
# Filters can be passed to a data request to get only specific data from a layer.
netzmacht.contao_leaflet.filter.bbox:
class: Netzmacht\Contao\Leaflet\Filter\BboxFilter
tags:
- { name: 'netzmacht.contao_leaflet.filter', alias: 'bbox' }
netzmacht.contao_leaflet.filter.distance:
class: Netzmacht\Contao\Leaflet\Filter\DistanceFilter
tags:
- { name: 'netzmacht.contao_leaflet.filter', alias: 'distance' }

View File

@@ -14,7 +14,7 @@ services:
- '@netzmacht.contao_toolkit.contao.input_adapter' - '@netzmacht.contao_toolkit.contao.input_adapter'
- '@netzmacht.contao_leaflet.map.assets' - '@netzmacht.contao_leaflet.map.assets'
- '@netzmacht.contao_leaflet.cache' - '@netzmacht.contao_leaflet.cache'
- [] # TODO: Rework filter handling. - '@netzmacht.contao_leaflet.filter_factory'
- '%kernel.debug%' - '%kernel.debug%'
netzmacht.contao_leaflet.libraries: netzmacht.contao_leaflet.libraries:
@@ -22,6 +22,11 @@ services:
arguments: arguments:
- '@contao.framework' - '@contao.framework'
netzmacht.contao_leaflet.filter_factory:
class: Netzmacht\Contao\Leaflet\Filter\FilterFactory
arguments:
- '%netzmacht.contao_leaflet.filters%'
netzmacht.contao_leaflet.cache: netzmacht.contao_leaflet.cache:
alias: 'netzmacht.contao_leaflet.cache.default' alias: 'netzmacht.contao_leaflet.cache.default'

View File

@@ -0,0 +1,59 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
* @filesource
*/
declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Filter;
/**
* Class FilterFactory.
*
* @package Netzmacht\Contao\Leaflet\Filter
*/
final class FilterFactory
{
/**
* Map of filter classes.
*
* @var array
*/
private $filters;
/**
* FilterFactory constructor.
*
* @param array $filters Map of filter classes.
*/
public function __construct(array $filters)
{
$this->filters = $filters;
}
/**
* Create a filter.
*
* @param string $filter Filter name.
* @param string $values Filter values.
*
* @return Filter
*
* @throws \RuntimeException When filter is not supported.
*/
public function create(string $filter, string $values): Filter
{
if (isset($this->filters[$filter])) {
return call_user_func([$filter, 'fromRequest'], $values);
}
throw new \RuntimeException(sprintf('Creating filter failed. Unsupported filter "%s"', $filter));
}
}

View File

@@ -13,6 +13,7 @@
namespace Netzmacht\Contao\Leaflet\Frontend; namespace Netzmacht\Contao\Leaflet\Frontend;
use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Filter\FilterFactory;
use Netzmacht\Contao\Leaflet\MapProvider; use Netzmacht\Contao\Leaflet\MapProvider;
/** /**
@@ -42,13 +43,6 @@ class DataController
'values' => null 'values' => null
); );
/**
* Filters configuration.
*
* @var array
*/
private $filters;
/** /**
* Display errors. * Display errors.
* *
@@ -56,18 +50,25 @@ class DataController
*/ */
private $displayErrors; private $displayErrors;
/**
* Filter factory.
*
* @var FilterFactory
*/
private $filterFactory;
/** /**
* Construct. * Construct.
* *
* @param MapProvider $mapProvider The map provider. * @param MapProvider $mapProvider The map provider.
* @param array $filters Filters configuration. * @param FilterFactory $filterFactory Filter factory.
* @param bool $displayErrors Display errors. * @param bool $displayErrors Display errors.
*/ */
public function __construct(MapProvider $mapProvider, array $filters, $displayErrors) public function __construct(MapProvider $mapProvider, FilterFactory $filterFactory, $displayErrors)
{ {
$this->mapProvider = $mapProvider; $this->mapProvider = $mapProvider;
$this->filters = $filters;
$this->displayErrors = $displayErrors; $this->displayErrors = $displayErrors;
$this->filterFactory = $filterFactory;
} }
/** /**
@@ -85,7 +86,7 @@ class DataController
try { try {
if ($input['filter']) { if ($input['filter']) {
$filter = $this->createFilter($input); $filter = $this->filterFactory->create($input['filter'], $input['values']);
} else { } else {
$filter = null; $filter = null;
} }
@@ -152,26 +153,4 @@ class DataController
return array($data, $error); return array($data, $error);
} }
/**
* Create a filter.
*
* @param array $input The user input as array.
*
* @return Filter
* @throws \RuntimeException If the filter is not defined.
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function createFilter($input)
{
if (!isset($this->filters[$input['filter']])) {
throw new \RuntimeException(sprintf('Undefined filter "%s".', $input['filter']));
}
/** @var Filter $filter */
$filter = $this->filters[$input['filter']];
return $filter::fromRequest($input['values']);
}
} }

View File

@@ -16,6 +16,7 @@ use Contao\Input;
use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Cache;
use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent;
use Netzmacht\Contao\Leaflet\Filter\Filter; use Netzmacht\Contao\Leaflet\Filter\Filter;
use Netzmacht\Contao\Leaflet\Filter\FilterFactory;
use Netzmacht\Contao\Leaflet\Frontend\DataController; use Netzmacht\Contao\Leaflet\Frontend\DataController;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
@@ -69,11 +70,11 @@ class MapProvider
private $assets; private $assets;
/** /**
* Request filters configuration. * Filter factory.
* *
* @var array * @var FilterFactory
*/ */
private $filters; private $filterFactory;
/** /**
* Display errors setting. * Display errors setting.
@@ -98,7 +99,7 @@ class MapProvider
* @param Input $input Thw request input. * @param Input $input Thw request input.
* @param ContaoAssets $assets Assets handler. * @param ContaoAssets $assets Assets handler.
* @param Cache $cache Cache. * @param Cache $cache Cache.
* @param array $filters Request filters configuration. * @param FilterFactory $filterFactory Filter factory.
* @param bool $displayErrors Display errors setting. * @param bool $displayErrors Display errors setting.
*/ */
public function __construct( public function __construct(
@@ -108,7 +109,7 @@ class MapProvider
$input, $input,
ContaoAssets $assets, ContaoAssets $assets,
Cache $cache, Cache $cache,
array $filters, FilterFactory $filterFactory,
$displayErrors $displayErrors
) { ) {
$this->mapper = $mapper; $this->mapper = $mapper;
@@ -116,7 +117,7 @@ class MapProvider
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->input = $input; $this->input = $input;
$this->assets = $assets; $this->assets = $assets;
$this->filters = $filters; $this->filterFactory = $filterFactory;
$this->displayErrors = $displayErrors; $this->displayErrors = $displayErrors;
$this->cache = $cache; $this->cache = $cache;
} }
@@ -293,7 +294,7 @@ class MapProvider
return; return;
} }
$controller = new DataController($this, $this->filters, $this->displayErrors); $controller = new DataController($this, $this->filterFactory, $this->displayErrors);
$controller->execute($data); $controller->execute($data);
if ($exit) { if ($exit) {