Extract parameter parsing from class.

This commit is contained in:
Malte Hübner 2017-05-26 16:16:40 +02:00
parent 27da5ba253
commit 5a7bdc21b1
2 changed files with 59 additions and 59 deletions

View File

@ -59,9 +59,12 @@ class Printer
protected $mapCacheFile = ''; protected $mapCacheFile = '';
protected $mapCacheExtension = 'png'; protected $mapCacheExtension = 'png';
protected $zoom, $lat, $lon, $width, $height, $markers, $polylines, $image, $maptype; protected $zoom, $lat, $lon, $width, $height, $image, $maptype;
protected $centerX, $centerY, $offsetX, $offsetY; protected $centerX, $centerY, $offsetX, $offsetY;
protected $markers = [];
protected $polylines = [];
public function __construct() public function __construct()
{ {
$this->zoom = 0; $this->zoom = 0;
@ -69,80 +72,67 @@ class Printer
$this->lon = 0; $this->lon = 0;
$this->width = 500; $this->width = 500;
$this->height = 350; $this->height = 350;
$this->markers = array();
$this->maptype = $this->tileDefaultSrc; $this->maptype = $this->tileDefaultSrc;
} }
public function parseParams() public function addMarker(string $markerType, float $latitude, float $longitude): Printer
{ {
global $_GET; $marker = ['lat' => $latitude, 'lon' => $longitude, 'type' => $markerType];
if (!empty($_GET['show'])) { array_push($this->markers, $marker);
$this->parseOjwParams();
} return $this;
else {
$this->parseLiteParams();
}
} }
public function parseLiteParams() public function addPolyline(string $polylineString, int $colorRed, int $colorGreen, int $colorBlue): Printer
{ {
// get zoom from GET paramter $polyline = ['polyline' => $polylineString, 'colorRed' => $colorRed, 'colorGreen' => $colorGreen, 'colorBlue' => $colorBlue];
$this->zoom = $_GET['zoom'] ? intval($_GET['zoom']) : 0;
if ($this->zoom > 18) $this->zoom = 18;
// get lat and lon from GET paramter array_push($this->polylines, $polyline);
list($this->lat, $this->lon) = explode(',', $_GET['center']);
$this->lat = floatval($this->lat);
$this->lon = floatval($this->lon);
// get size from GET paramter return $this;
if ($_GET['size']) {
list($this->width, $this->height) = explode('x', $_GET['size']);
$this->width = intval($this->width);
if ($this->width > $this->maxWidth) $this->width = $this->maxWidth;
$this->height = intval($this->height);
if ($this->height > $this->maxHeight) $this->height = $this->maxHeight;
}
if (!empty($_GET['markers'])) {
$markers = explode('|', $_GET['markers']);
foreach ($markers as $marker) {
list($markerLat, $markerLon, $markerType) = explode(',', $marker);
$markerLat = floatval($markerLat);
$markerLon = floatval($markerLon);
$markerType = basename($markerType);
$this->markers[] = array('lat' => $markerLat, 'lon' => $markerLon, 'type' => $markerType);
} }
} public function setCenter(float $latitude, float $longitude): Printer
if (!empty($_GET['polylines'])) {
list($polylineString, $colorRed, $colorGreen, $colorBlue) = explode(',', $_GET['polylines']);
$this->polylines[] = array('polyline' => $polylineString, 'colorRed' => $colorRed, 'colorGreen' => $colorGreen, 'colorBlue' => $colorBlue);
}
if ($_GET['maptype']) {
if (array_key_exists($_GET['maptype'], $this->tileSrcUrl)) $this->maptype = $_GET['maptype'];
}
}
public function parseOjwParams()
{ {
$this->lat = floatval($_GET['lat']); $this->lat = $latitude;
$this->lon = floatval($_GET['lon']); $this->lon = $longitude;
$this->zoom = intval($_GET['z']);
$this->width = intval($_GET['w']); return $this;
if ($this->width > $this->maxWidth) $this->width = $this->maxWidth;
$this->height = intval($_GET['h']);
if ($this->height > $this->maxHeight) $this->height = $this->maxHeight;
if (!empty($_GET['mlat0'])) {
$markerLat = floatval($_GET['mlat0']);
if (!empty($_GET['mlon0'])) {
$markerLon = floatval($_GET['mlon0']);
$this->markers[] = array('lat' => $markerLat, 'lon' => $markerLon, 'type' => "bullseye");
} }
public function setSize(int $width, int $height): Printer
{
$this->width = $width;
$this->height = $height;
if ($this->width > $this->maxWidth) {
$this->width = $this->maxWidth;
} }
if ($this->height > $this->maxHeight) {
$this->height = $this->maxHeight;
}
return $this;
}
public function setZoom(int $zoom): Printer
{
$this->zoom = $zoom;
if ($this->zoom > 18) {
$this->zoom = 18;
}
return $this;
}
public function setMapType(string $mapType): Printer
{
$this->maptype = $mapType;
return $this;
} }
public function lonToTile($long, $zoom) public function lonToTile($long, $zoom)
@ -399,7 +389,6 @@ class Printer
public function showMap() public function showMap()
{ {
$this->parseParams();
if ($this->useMapCache) { if ($this->useMapCache) {
// use map cache, so check cache for map // use map cache, so check cache for map
if (!$this->checkMapCache()) { if (!$this->checkMapCache()) {

View File

@ -30,4 +30,15 @@ require_once '../vendor/autoload.php';
use StaticMapLite\Printer; use StaticMapLite\Printer;
$printer = new Printer(); $printer = new Printer();
list($centerLatitude, $centerLongitude) = explode(',', $_GET['center']);
list($width, $height) = explode('x', $_GET['size']);
$printer
->setCenter($centerLatitude, $centerLongitude)
->setZoom($_GET['zoom'])
->setSize($width, $height)
->setMapType($_GET['maptype'])
;
print $printer->showMap(); print $printer->showMap();