diff --git a/src/Bundle/DependencyInjection/NetzmachtContaoLeafletExtension.php b/src/Bundle/DependencyInjection/NetzmachtContaoLeafletExtension.php index 98b88fb..6e56b9c 100644 --- a/src/Bundle/DependencyInjection/NetzmachtContaoLeafletExtension.php +++ b/src/Bundle/DependencyInjection/NetzmachtContaoLeafletExtension.php @@ -46,7 +46,6 @@ class NetzmachtContaoLeafletExtension extends Extension $loader->load('providers.yml'); // Other services - $loader->load('filters.yml'); $loader->load('mappers.yml'); $loader->load('encoders.yml'); $loader->load('layers.yml'); diff --git a/src/Bundle/Resources/config/config.yml b/src/Bundle/Resources/config/config.yml index bbc7d26..41da04e 100644 --- a/src/Bundle/Resources/config/config.yml +++ b/src/Bundle/Resources/config/config.yml @@ -58,3 +58,8 @@ parameters: - 'id' - 'title' - '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 diff --git a/src/Bundle/Resources/config/filters.yml b/src/Bundle/Resources/config/filters.yml deleted file mode 100644 index 7f3cb2e..0000000 --- a/src/Bundle/Resources/config/filters.yml +++ /dev/null @@ -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' } diff --git a/src/Bundle/Resources/config/services.yml b/src/Bundle/Resources/config/services.yml index 6927752..e5c8290 100644 --- a/src/Bundle/Resources/config/services.yml +++ b/src/Bundle/Resources/config/services.yml @@ -14,7 +14,7 @@ services: - '@netzmacht.contao_toolkit.contao.input_adapter' - '@netzmacht.contao_leaflet.map.assets' - '@netzmacht.contao_leaflet.cache' - - [] # TODO: Rework filter handling. + - '@netzmacht.contao_leaflet.filter_factory' - '%kernel.debug%' netzmacht.contao_leaflet.libraries: @@ -22,6 +22,11 @@ services: arguments: - '@contao.framework' + netzmacht.contao_leaflet.filter_factory: + class: Netzmacht\Contao\Leaflet\Filter\FilterFactory + arguments: + - '%netzmacht.contao_leaflet.filters%' + netzmacht.contao_leaflet.cache: alias: 'netzmacht.contao_leaflet.cache.default' diff --git a/src/Filter/FilterFactory.php b/src/Filter/FilterFactory.php new file mode 100644 index 0000000..9909db9 --- /dev/null +++ b/src/Filter/FilterFactory.php @@ -0,0 +1,59 @@ + + * @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)); + } +} diff --git a/src/Frontend/DataController.php b/src/Frontend/DataController.php index abf8eca..b15fba5 100644 --- a/src/Frontend/DataController.php +++ b/src/Frontend/DataController.php @@ -13,6 +13,7 @@ namespace Netzmacht\Contao\Leaflet\Frontend; use Netzmacht\Contao\Leaflet\Filter\Filter; +use Netzmacht\Contao\Leaflet\Filter\FilterFactory; use Netzmacht\Contao\Leaflet\MapProvider; /** @@ -41,13 +42,6 @@ class DataController 'filter' => null, 'values' => null ); - - /** - * Filters configuration. - * - * @var array - */ - private $filters; /** * Display errors. @@ -56,18 +50,25 @@ class DataController */ private $displayErrors; + /** + * Filter factory. + * + * @var FilterFactory + */ + private $filterFactory; + /** * Construct. * - * @param MapProvider $mapProvider The map provider. - * @param array $filters Filters configuration. - * @param bool $displayErrors Display errors. + * @param MapProvider $mapProvider The map provider. + * @param FilterFactory $filterFactory Filter factory. + * @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->filters = $filters; $this->displayErrors = $displayErrors; + $this->filterFactory = $filterFactory; } /** @@ -85,7 +86,7 @@ class DataController try { if ($input['filter']) { - $filter = $this->createFilter($input); + $filter = $this->filterFactory->create($input['filter'], $input['values']); } else { $filter = null; } @@ -152,26 +153,4 @@ class DataController 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']); - } } diff --git a/src/MapProvider.php b/src/MapProvider.php index 27ba28e..41f2ab5 100644 --- a/src/MapProvider.php +++ b/src/MapProvider.php @@ -16,6 +16,7 @@ use Contao\Input; use Doctrine\Common\Cache\Cache; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Filter\Filter; +use Netzmacht\Contao\Leaflet\Filter\FilterFactory; use Netzmacht\Contao\Leaflet\Frontend\DataController; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Model\LayerModel; @@ -69,11 +70,11 @@ class MapProvider private $assets; /** - * Request filters configuration. + * Filter factory. * - * @var array + * @var FilterFactory */ - private $filters; + private $filterFactory; /** * Display errors setting. @@ -98,7 +99,7 @@ class MapProvider * @param Input $input Thw request input. * @param ContaoAssets $assets Assets handler. * @param Cache $cache Cache. - * @param array $filters Request filters configuration. + * @param FilterFactory $filterFactory Filter factory. * @param bool $displayErrors Display errors setting. */ public function __construct( @@ -108,7 +109,7 @@ class MapProvider $input, ContaoAssets $assets, Cache $cache, - array $filters, + FilterFactory $filterFactory, $displayErrors ) { $this->mapper = $mapper; @@ -116,7 +117,7 @@ class MapProvider $this->eventDispatcher = $eventDispatcher; $this->input = $input; $this->assets = $assets; - $this->filters = $filters; + $this->filterFactory = $filterFactory; $this->displayErrors = $displayErrors; $this->cache = $cache; } @@ -293,7 +294,7 @@ class MapProvider return; } - $controller = new DataController($this, $this->filters, $this->displayErrors); + $controller = new DataController($this, $this->filterFactory, $this->displayErrors); $controller->execute($data); if ($exit) {