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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Nov 11 03:37:25 EST 2010


Author: christoph
Date: 2010-11-11 00:37:25 -0800 (Thu, 11 Nov 2010)
New Revision: 7106

Modified:
   trunk/mapbender/lib/class_OgcFilter.php
   trunk/mapbender/test/http/classes/OgcFilterTest.php
Log:
class for creating complex OGC Filter conditions (based on Mapbender filter class)

Modified: trunk/mapbender/lib/class_OgcFilter.php
===================================================================
--- trunk/mapbender/lib/class_OgcFilter.php	2010-11-11 08:34:20 UTC (rev 7105)
+++ trunk/mapbender/lib/class_OgcFilter.php	2010-11-11 08:37:25 UTC (rev 7106)
@@ -19,34 +19,75 @@
 class OgcFilter extends Filter {
 	const SPATIAL_OPERATORS = "Intersects";
 
+	private function mapOperator ($op) {
+		switch ($op) {
+			case "LIKE":
+				return array(
+					"open" => 'ogc:PropertyIsLike wildCard="*" singleChar="#" escapeChar="!"',
+					"close" => 'ogc:PropertyIsLike'
+				);
+				break;
+			case "AND":
+				return array(
+					"open" => 'ogc:And',
+					"close" => 'ogc:And'
+				);
+				break;
+			default:
+				return array(
+					"open" => $op,
+					"close" => $op
+				);
+		}
+	}
 	public function __construct () {
 		$allOperators = implode(",", array(self::OPERATORS, self::SPATIAL_OPERATORS));
-		if (func_num_args() === 4) {
+		if (func_num_args() >= 3) {
 			$this->operator = func_get_arg(0);
 			$this->key = func_get_arg(1);
 			$this->value = func_get_arg(2);
-			$this->wfsConf = func_get_arg(3);
+			if (func_num_args() >= 4) {
+				$this->wfsConf = func_get_arg(3);
+				if (!is_a($this->wfsConf, "WfsConfiguration")) {
+					throw new Exception ("OgcFilter: wfsConf is not a WFS Configuration.");
+				}
+			}
 			if (!in_array($this->operator, explode(",", $allOperators))) {
 				throw new Exception ("OgcFilter: Invalid operator " . $this->operator);
 			}
-			if (!is_a($this->wfsConf, "WfsConfiguration")) {
-				throw new Exception ("OgcFilter: wfsConf is not a WFS Configuration.");
-			}
-
 		}
+		else if (func_num_args() === 2) {
+			$logicalOp = func_get_arg(0);
+			$filterArray = func_get_arg(1);
+			return parent::__construct($logicalOp, $filterArray);
+		}
 		else {
 			throw new Exception("OgcFilter: Insufficient arguments.");
 		}
 	}
 
 	public function toXmlNoWrap() {
-		$k = "<ogc:PropertyName>" . $this->key . "</ogc:PropertyName>";
-		if (in_array($this->operator, explode(",", self::SPATIAL_OPERATORS))) {
-			$gmlFactory = new UniversalGmlFactory();
-			$gml = $gmlFactory->createFromGeoJson($this->value, $this->wfsConf);
-			$v = $gml->toGml();
+		if ($this->isComplex()) {
+			$str = "";
+			foreach ($this->filterArray as $filter) {
+				$str .= $filter->toXmlNoWrap();
+			}
+			$op = $this->mapOperator($this->booleanOperator);
+			return "<" . $op["open"] . ">" . $str . "</" . $op["close"] . ">";
 		}
-		return "<" . $this->operator . ">" . $k . $v . "</" . $this->operator . ">";
+		else {
+			$k = "<ogc:PropertyName>" . $this->key . "</ogc:PropertyName>";
+			if (in_array($this->operator, explode(",", self::SPATIAL_OPERATORS))) {
+				$gmlFactory = new UniversalGmlFactory();
+				$gml = $gmlFactory->createFromGeoJson($this->value, $this->wfsConf);
+				$v = $gml->toGml();
+			}
+			else {
+				$v = "<ogc:Literal>" . $this->value . "</ogc:Literal>";
+			}
+			$op = $this->mapOperator($this->operator);
+			return "<" . $op["open"] . ">" . $k . $v . "</" . $op["close"] . ">";
+		}
 	}
 	
 	public function toXml () {

Modified: trunk/mapbender/test/http/classes/OgcFilterTest.php
===================================================================
--- trunk/mapbender/test/http/classes/OgcFilterTest.php	2010-11-11 08:34:20 UTC (rev 7105)
+++ trunk/mapbender/test/http/classes/OgcFilterTest.php	2010-11-11 08:37:25 UTC (rev 7106)
@@ -14,6 +14,13 @@
 		$this->fail('An expected Exception has not been raised.');
 	}
 
+	private function removeWhiteSpace ($string) {
+		$str = preg_replace("/\>(\s)+\</", "><", trim($string));
+//		$str = preg_replace("/\\n/", "\\n", $str);
+		return $str;
+
+	}
+
 	public function testInvalidOperator () {
 		try {
 			$filter = new OgcFilter("gsdfhj", "a", "b", new WfsConfiguration());
@@ -34,6 +41,43 @@
 		$this->fail('An expected Exception has not been raised.');
 	}
 
+	public function testPropertyIsLike () {
+		$filter = new OgcFilter("LIKE", "a", "3*");
+		$expected = <<<FILTER
+<ogc:Filter>
+	<ogc:PropertyIsLike wildCard="*" singleChar="#" escapeChar="!">
+		<ogc:PropertyName>a</ogc:PropertyName>
+		<ogc:Literal>3*</ogc:Literal>
+	</ogc:PropertyIsLike>
+</ogc:Filter>
+FILTER;
+
+		$this->assertEquals($this->removeWhiteSpace($expected), $this->removeWhiteSpace($filter->toXml()));
+	}
+
+	public function testPropertyIsLikeAnd () {
+		$filter1 = new OgcFilter("LIKE", "a", "3*");
+		$filter2 = new OgcFilter("LIKE", "b", "*asd");
+		$filter = new OgcFilter("AND", array($filter1, $filter2));
+		$expected = <<<FILTER
+<ogc:Filter>
+	<ogc:And>
+		<ogc:PropertyIsLike wildCard="*" singleChar="#" escapeChar="!">
+			<ogc:PropertyName>a</ogc:PropertyName>
+			<ogc:Literal>3*</ogc:Literal>
+		</ogc:PropertyIsLike>
+		<ogc:PropertyIsLike wildCard="*" singleChar="#" escapeChar="!">
+			<ogc:PropertyName>b</ogc:PropertyName>
+			<ogc:Literal>*asd</ogc:Literal>
+		</ogc:PropertyIsLike>
+	</ogc:And>
+</ogc:Filter>
+FILTER;
+
+		$this->assertEquals($this->removeWhiteSpace($expected), $this->removeWhiteSpace($filter->toXml()));
+	}
+
+
 	// this test is not operational
 /*
 	public function testOperatorIntersects () {



More information about the Mapbender_commits mailing list