[Mapbender-commits] r6873 - in trunk/mapbender: http/javascripts lib test/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Sep 2 04:50:48 EDT 2010


Author: christoph
Date: 2010-09-02 08:50:48 +0000 (Thu, 02 Sep 2010)
New Revision: 6873

Added:
   trunk/mapbender/lib/class_GetApi.php
   trunk/mapbender/test/http/classes/GetApiTest.php
Modified:
   trunk/mapbender/http/javascripts/initWmcObj.php
Log:
added new class to normalize the GET input 

Modified: trunk/mapbender/http/javascripts/initWmcObj.php
===================================================================
--- trunk/mapbender/http/javascripts/initWmcObj.php	2010-09-02 08:49:18 UTC (rev 6872)
+++ trunk/mapbender/http/javascripts/initWmcObj.php	2010-09-02 08:50:48 UTC (rev 6873)
@@ -2,9 +2,10 @@
 //
 // Load WMS via WMC
 // 
-require_once(dirname(__FILE__)."/../php/mb_validateSession.php");
-require_once(dirname(__FILE__)."/../classes/class_wmc.php");
-require_once(dirname(__FILE__)."/../classes/class_administration.php");
+require_once dirname(__FILE__)."/../php/mb_validateSession.php";
+require_once dirname(__FILE__)."/../classes/class_wmc.php";
+require_once dirname(__FILE__)."/../classes/class_administration.php";
+require_once dirname(__FILE__)."/../../lib/class_GetApi.php";
 
 $wmc = new wmc();
 
@@ -36,7 +37,12 @@
 	$options["zoom"] = !!Mapbender::session()->get("addwms_zoomToExtent");
 }
 
-$getParams["WMS"] = getConfiguration("WMS");
+$getParams = array(
+	"WMS" => getConfiguration("WMS"),
+	"LAYER" => getConfiguration("LAYER"),
+	"FEATURETYPE" => getConfiguration("FEATURETYPE")
+);
+$getApi = new GetApi($getParams);
 
 if ($getParams['WMS']) {
 	// WMS param given as array
@@ -139,68 +145,8 @@
 	}
 }
 
-$getParams["LAYER"] = getConfiguration("LAYER");
-
-// for possible inputs see http://www.mapbender.org/GET-Parameter#LAYER
-// for test cases, see http://www.mapbender.org/Talk:GET-Parameter#LAYER
-function normalizeLayerInput ($input) {
-	if (is_array($input)) {
-		$keys = array_keys($input);
-		$isSingleLayer = false;
-		foreach ($keys as $key) {
-			if (!is_numeric($key)) {
-				$isSingleLayer = true;
-				break;
-			}
-		}
-		// LAYER[id]=12&LAYER[application]=something
-		if ($isSingleLayer) {
-			$input[0] = array();
-			foreach ($keys as $key) {
-				if (!is_numeric($key)) {
-					$input[0][$key] = $input[$key];
-					unset($input[$key]);
-				}
-			}
-		}
-		else {
-			for ($i = 0; $i < count($input); $i++) {
-				// assume LAYER[]=12&LAYER[]=13
-				if (is_numeric($input[$i])) {
-					$id = $input[$i];
-					$input[$i] = array("id" => $id);
-				}
-				// else assume LAYER[0][id]=12&LAYER[0][application]=something
-			}
-		}
-	}
-	else {
-		// assume LAYER=12,13,14
-		$inputArray = split(",", $input); 
-		$input = array();
-		$i = 0;
-		foreach ($inputArray as $id) {
-			if (is_numeric($id)) {
-				$input[$i++]["id"] = $id;
-			}
-		}
-	}
-	// check if each layer has at least an id, if not, delete
-	$i = 0;
-	while ($i < count($input)) {
-		if (!is_array($input[$i]) || !isset($input[$i]["id"]) || !is_numeric($input[$i]["id"])) {
-			array_splice($input, $i, 1);
-			continue;
-		}
-		$input[$i]["id"] = intval($input[$i]["id"]);
-		$i++;
-	}
-	return $input;
-}
-
-if ($getParams["LAYER"]) {
-	$inputLayerArray = normalizeLayerInput($getParams['LAYER']);
-
+$inputLayerArray = $getApi->getLayers();
+if ($inputLayerArray) {
 	foreach ($inputLayerArray as $input) {
 		// just make it work for a single layer id
 		$wmsFactory = new UniversalWmsFactory();
@@ -215,85 +161,26 @@
 	}
 }
 
-$getParams["FEATURETYPE"] = getConfiguration("FEATURETYPE");
-
-function normalizeFeaturetypeInput ($input) {
-	if (is_array($input)) {
-		$keys = array_keys($input);
-		$isSingleFeaturetype = false;
-		foreach ($keys as $key) {
-			if (!is_numeric($key)) {
-				$isSingleFeaturetype = true;
-				break;
-			}
-		}
-		// FEATURETYPE[id]=12&FEATURETYPE[active]=something
-		if ($isSingleFeaturetype) {
-			$input[0] = array();
-			foreach ($keys as $key) {
-				if (!is_numeric($key)) {
-					$input[0][$key] = $input[$key];
-					unset($input[$key]);
-				}
-			}
-		}
-		else {
-			for ($i = 0; $i < count($input); $i++) {
-				// assume FEATURETYPE[]=12&FEATURETYPE[]=13
-				if (is_numeric($input[$i])) {
-					$id = $input[$i];
-					$input[$i] = array("id" => $id);
-				}
-			}
-		}
-	}
-	else {
-		// assume FEATURETYPE=12,13,14
-		$inputArray = split(",", $input); 
-		$input = array();
-		$i = 0;
-		foreach ($inputArray as $id) {
-			if (is_numeric($id)) {
-				$input[$i++]["id"] = $id;
-			}
-		}
-	}
-	// check if each featuretype has at least an id, if not, delete
-	$i = 0;
-	while ($i < count($input)) {
-		if (!is_array($input[$i]) || !isset($input[$i]["id"]) || !is_numeric($input[$i]["id"])) {
-			array_splice($input, $i, 1);
-			continue;
-		}
-		$input[$i]["id"] = intval($input[$i]["id"]);
-		$i++;
-	}
-	return $input;
-}
-
-if ($getParams["FEATURETYPE"]) {
-	$inputFeaturetypeArray = normalizeFeaturetypeInput($getParams['FEATURETYPE']);
-
+$inputFeaturetypeArray = $getApi->getFeaturetypes();
+if ($inputFeaturetypeArray) {
 	$wfsConfIds = array();	
 	foreach ($inputFeaturetypeArray as $input) {
 		array_push($wfsConfIds, $input["id"]);
 	}
 	
 	$wmc->generalExtensionArray['WFSCONFIDSTRING'] = implode(",", array_merge(
-		explode(",", $wmc->generalExtensionArray['WFSCONFIDSTRING']),
+		$wmc->generalExtensionArray['WFSCONFIDSTRING'] ? 
+			explode(",", $wmc->generalExtensionArray['WFSCONFIDSTRING']) : array(),
 		$wfsConfIds
 	));
-	//$firephp->log($wmc->generalExtensionArray['WFSCONFIDSTRING']);
 }
 
 //workaround to have a fully merged WMC for loading
 
 $xml = $wmc->toXml();
-#$firephp->log($xml);
 
 $wmc = new wmc();
 $wmc->createFromXml($xml);
-//$firephp->log($wmc);
 
 $output = $wmc->wmsToJavaScript();
 
@@ -307,9 +194,13 @@
 }
 
 $wmcFeaturetypeJson = $wmc->featuretypeConfToJavaScript();
-$wmcFeaturetypeStr = "Mapbender.events.afterInit.register(function () {
-	$('#body').trigger('addFeaturetypeConfs', [{featuretypeConfObj : ".$wmcFeaturetypeJson."}]);
-});";
+$wmcFeaturetypeStr = <<<JS
+	Mapbender.events.afterInit.register(function () {
+		$('#body').trigger('addFeaturetypeConfs', [
+			{featuretypeConfObj : $wmcFeaturetypeJson}
+		]);
+	});
+JS;
 
 $outputString .= $wmcFeaturetypeStr;
 

Added: trunk/mapbender/lib/class_GetApi.php
===================================================================
--- trunk/mapbender/lib/class_GetApi.php	                        (rev 0)
+++ trunk/mapbender/lib/class_GetApi.php	2010-09-02 08:50:48 UTC (rev 6873)
@@ -0,0 +1,163 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License 
+# and Simplified BSD license.  
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once dirname(__FILE__)."/../core/globalSettings.php";
+
+/**
+ * Normalizes the input data specified at http://www.mapbender.org/GET-Parameter
+ */
+class GetApi {
+	private $layers = array();
+	private $featuretypes = array();
+	
+	/**
+	 * @param array $input
+	 */
+	public function __construct ($input) {
+		if (!is_array($input)) {
+			return null;
+		}
+		foreach ($input as $key => $value) {
+			switch ($key) {
+				case "LAYER":
+					$this->layers = $this->normalizeLayerInput($value);
+					break;
+				case "FEATURETYPE":
+					$this->featuretypes = $this->normalizeFeaturetypeInput($value);
+			}
+		}
+	}
+	
+	/**
+	 * Returns an array of layer metadata
+	 * @return array
+	 */
+	public function getLayers () {
+		return $this->layers;	
+	}
+
+	/**
+	 * Returns an array of featuretype metadata
+	 * @return array
+	 */
+	public function getFeaturetypes () {
+		return $this->featuretypes;	
+	}
+
+	// for possible inputs see http://www.mapbender.org/GET-Parameter#LAYER
+	// for test cases, see http://www.mapbender.org/Talk:GET-Parameter#LAYER
+	private function normalizeLayerInput ($input) {
+		if (is_array($input)) {
+			$keys = array_keys($input);
+			$isSingleLayer = false;
+			foreach ($keys as $key) {
+				if (!is_numeric($key)) {
+					$isSingleLayer = true;
+					break;
+				}
+			}
+			// LAYER[id]=12&LAYER[application]=something
+			if ($isSingleLayer) {
+				$input[0] = array();
+				foreach ($keys as $key) {
+					if (!is_numeric($key)) {
+						$input[0][$key] = $input[$key];
+						unset($input[$key]);
+					}
+				}
+			}
+			else {
+				for ($i = 0; $i < count($input); $i++) {
+					// assume LAYER[]=12&LAYER[]=13
+					if (is_numeric($input[$i])) {
+						$id = $input[$i];
+						$input[$i] = array("id" => $id);
+					}
+					// else assume LAYER[0][id]=12&LAYER[0][application]=something
+				}
+			}
+		}
+		else {
+			// assume LAYER=12,13,14
+			$inputArray = split(",", $input); 
+			$input = array();
+			$i = 0;
+			foreach ($inputArray as $id) {
+				if (is_numeric($id)) {
+					$input[$i++]["id"] = $id;
+				}
+			}
+		}
+		// check if each layer has at least an id, if not, delete
+		$i = 0;
+		while ($i < count($input)) {
+			if (!is_array($input[$i]) || !isset($input[$i]["id"]) || !is_numeric($input[$i]["id"])) {
+				array_splice($input, $i, 1);
+				continue;
+			}
+			$input[$i]["id"] = intval($input[$i]["id"]);
+			$i++;
+		}
+		return $input;
+	}
+
+	private function normalizeFeaturetypeInput ($input) {
+		if (is_array($input)) {
+			$keys = array_keys($input);
+			$isSingleFeaturetype = false;
+			foreach ($keys as $key) {
+				if (!is_numeric($key)) {
+					$isSingleFeaturetype = true;
+					break;
+				}
+			}
+			// FEATURETYPE[id]=12&FEATURETYPE[active]=something
+			if ($isSingleFeaturetype) {
+				$input[0] = array();
+				foreach ($keys as $key) {
+					if (!is_numeric($key)) {
+						$input[0][$key] = $input[$key];
+						unset($input[$key]);
+					}
+				}
+			}
+			else {
+				for ($i = 0; $i < count($input); $i++) {
+					// assume FEATURETYPE[]=12&FEATURETYPE[]=13
+					if (is_numeric($input[$i])) {
+						$id = $input[$i];
+						$input[$i] = array("id" => $id);
+					}
+				}
+			}
+		}
+		else {
+			// assume FEATURETYPE=12,13,14
+			$inputArray = split(",", $input); 
+			$input = array();
+			$i = 0;
+			foreach ($inputArray as $id) {
+				if (is_numeric($id)) {
+					$input[$i++]["id"] = $id;
+				}
+			}
+		}
+		// check if each featuretype has at least an id, if not, delete
+		$i = 0;
+		while ($i < count($input)) {
+			if (!is_array($input[$i]) || !isset($input[$i]["id"]) || !is_numeric($input[$i]["id"])) {
+				array_splice($input, $i, 1);
+				continue;
+			}
+			$input[$i]["id"] = intval($input[$i]["id"]);
+			$i++;
+		}
+		return $input;
+	}
+}
+
+?>
\ No newline at end of file

Added: trunk/mapbender/test/http/classes/GetApiTest.php
===================================================================
--- trunk/mapbender/test/http/classes/GetApiTest.php	                        (rev 0)
+++ trunk/mapbender/test/http/classes/GetApiTest.php	2010-09-02 08:50:48 UTC (rev 6873)
@@ -0,0 +1,124 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+require_once dirname(__FILE__) . "/../../../lib/class_GetApi.php";
+
+class GetApiTest extends PHPUnit_Framework_TestCase {
+
+	public function testSingleLayer () {
+		parse_str("LAYER=12", $getArray);
+
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getLayers());
+	}
+
+	public function testSingleLayerFromApplication () {
+		parse_str("LAYER[application]=gui1&LAYER[id]=12", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12,
+				"application" => "gui1"
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getLayers());
+	}
+
+	public function testMultipleLayer () {
+		parse_str("LAYER[0][application]=gui1&LAYER[0][id]=12&LAYER[1]=13", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12,
+				"application" => "gui1"
+			),
+			array(
+				"id" => 13
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getLayers());
+	}
+
+	public function testMultipleLayerComplex () {
+		parse_str("LAYER[visible]=false&LAYER[zoom]=true&LAYER[application]=gui1&LAYER[id]=12", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12,
+				"application" => "gui1",
+				"visible" => false,
+				"zoom" => true
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getLayers());
+	}
+	
+	public function testSingleFeaturetype () {
+		parse_str("FEATURETYPE=12", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getFeaturetypes());
+	}
+	
+	public function testMultipleFeaturetypes () {
+		parse_str("FEATURETYPE=12,13", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12
+			),
+			array(
+				"id" => 13
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getFeaturetypes());
+	}
+
+	public function testMultipleFeaturetypesArray () {
+		parse_str("FEATURETYPE[]=12&FEATURETYPE[]=13", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12
+			),
+			array(
+				"id" => 13
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getFeaturetypes());
+	}
+
+	public function testMultipleFeaturetypesArrayComplex () {
+		parse_str("FEATURETYPE[active]=false&FEATURETYPE[search][firstname]=a&FEATURETYPE[search][lastname]=b&FEATURETYPE[id]=12", $getArray);
+		$apiObject = new GetApi($getArray);
+		
+		$expected = array(
+			array(
+				"id" => 12,
+				"active" => false,
+				"search" => array(
+					"firstname" => "a",
+					"lastname" => "b"
+				)
+			)
+		);
+		$this->assertEquals($expected, $apiObject->getFeaturetypes());
+	}
+
+}
+?>
\ No newline at end of file



More information about the Mapbender_commits mailing list