From 27a01112c3f4c32cca8ad45c53620b1aaafe27b8 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Wed, 7 Jan 2015 09:38:05 +0100 Subject: [PATCH] Extract request url. --- composer.json | 1 + .../Mapper/Layer/MarkersLayerMapper.php | 4 +- .../Contao/Leaflet/Request/RequestUrl.php | 60 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/Netzmacht/Contao/Leaflet/Request/RequestUrl.php diff --git a/composer.json b/composer.json index 785ffdd..898c130 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "contao/core":">=3.1,<3.5-dev", "contao-community-alliance/dependency-container":"~1.0", "contao-community-alliance/event-dispatcher":"~1.0", + "contao-community-alliance/url-builder":"~1.1", "netzmacht/php-javascript-builder": "~1.0", "netzmacht/php-leaflet": "dev-master", "bit3/contao-meta-palettes": "~1.5", diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php index 4c3d83e..d2e0eaa 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/Layer/MarkersLayerMapper.php @@ -14,6 +14,7 @@ namespace Netzmacht\Contao\Leaflet\Mapper\Layer; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\GeoJsonMapper; use Netzmacht\Contao\Leaflet\Model\MarkerModel; +use Netzmacht\Contao\Leaflet\Request\RequestUrl; use Netzmacht\Javascript\Type\Value\Reference; use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; @@ -63,8 +64,7 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper LatLngBounds $bounds = null ) { if ($definition instanceof GeoJsonAjax) { - $base = \Config::get('websitePath') . '/assets/leaflet/geojson.php?id='; - $definition->setUrl($base . $model->id); + $definition->setUrl(RequestUrl::create($model->id)); } elseif ($definition instanceof LayerGroup) { $collection = $this->loadMarkerModels($model); diff --git a/src/Netzmacht/Contao/Leaflet/Request/RequestUrl.php b/src/Netzmacht/Contao/Leaflet/Request/RequestUrl.php new file mode 100644 index 0000000..0b5206c --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Request/RequestUrl.php @@ -0,0 +1,60 @@ + + * @copyright 2015 netzmacht creative David Molineus + * @license LGPL 3.0 + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Request; + +use ContaoCommunityAlliance\UrlBuilder\UrlBuilder; + +/** + * Class RequestUrl creates the request url. + * + * @package Netzmacht\Contao\Leaflet\Request + */ +class RequestUrl +{ + const BASE = 'assets/leaflet/data.php'; + + /** + * Create the request url. + * + * @param int $id Object id. + * @param string|null $type Object type. If empty it assumes a layer. + * @param string|null $format Data format. If empty it assumes geojson. + * + * @return string + */ + public static function create($id, $type = null, $format = null) + { + return self::createBuilder($id, $type, $format)->getUrl(); + } + + /** + * Create the request builder. + * + * @param int $id Object id. + * @param string|null $type Object type. If empty it assumes a layer. + * @param string|null $format Data format. If empty it assumes geojson. + * + * @return UrlBuilder + */ + public static function createBuilder($id, $type = null, $format = null) + { + $path = \Config::get('websitePath') . '/' . static::BASE; + $builder = new UrlBuilder(); + $builder + ->setPath($path) + ->setQueryParameter('type', $type ?: 'layer') + ->setQueryParameter('format', $format ?: 'geojson') + ->setQueryParameter('id', $id); + + return $builder; + } +}