Merge branch 'feature/#61' into devel

This commit is contained in:
stubbfel
2013-06-26 12:15:37 +02:00
11 changed files with 317 additions and 19 deletions

View File

@@ -26,12 +26,15 @@ abstract class Api extends \Slim\Slim {
public function __construct() {
$this->connect();
parent::__construct();
$this->contentType("Content-type: application/xml;charset=utf-8");
}
/**
* Default-DeConstructor
*/
public function __destruct() {
$this->sqlManager->closeConnection();
unset($this->sqlManager);
$this->sqlManager = null;
}

74
geoapi/api/PisApi.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
namespace api;
include_once "../../global.inc.php";
include_once PATH_DATABASE . "/PisSqlManager.php";
include_once PATH_UTILITTY . "/XmlManager.php";
require_once PATH_API . "/Api.php";
/**
* This class provides some spezial SpsAPI methods
* @author stubbfel
* @since 20.06.2013
*/
class PisApi extends Api {
/**
* Route string for the alias paramter
* @var string
*/
public static $routeParameterPids = "/pid/:pid+";
/**
* max number of pid for each query
* @var int
*/
private $maxPid = 10;
/**
* Default-Constructor
*/
public function __construct() {
$this->sqlManager = new \database\PisSqlManager();
parent::__construct();
}
/**
* Default-DeConstructor
*/
public function __destruct() {
parent::__destruct();
}
/**
* Method start a pis-query
* @param array $queryArgs
* @return query result as xml
*/
public function sendPisQuery($queryArgs) {
$pidList = $this->removeEmptyItmes($queryArgs);
if (count($pidList) < $this->maxPid) {
$result = $this->sqlManager->sendPisQuery($pidList);
return \utiliy\XmlManager::arrayToPisXml($result);
}
return NULL;
}
/**
* Method remove all empty string
* @param array $queryArgs
* @return array
*/
private function removeEmptyItmes($queryArgs) {
foreach ($queryArgs as $key => $value) {
if (empty($value)) {
unset($queryArgs[$key]);
}
}
return $queryArgs;
}
}
?>

View File

@@ -14,6 +14,30 @@ require_once PATH_API . "/Api.php";
*/
class SpsApi extends Api {
/**
* Route string for the alias paramter
* @var string
*/
public static $routeParameterAlias = "/alias/:alias";
/**
* Route string for the alias paramter
* @var string
*/
public static $routeParameterDomain = "/domain/:domain";
/**
* Route string for the Longitude paramter
* @var string
*/
public static $routeParameterLongitude= "/longitude/:longitude";
/**
* Route string for the latitude paramter
* @var string
*/
public static $routeParameterLatitude = "/latitude/:latitude";
/**
* Keyword for alias arguments
* @var string
@@ -107,7 +131,7 @@ class SpsApi extends Api {
* @param array $queryArgs
* @return querry result as xml
*/
public function sendSpsAliasQuery($queryArgs = array()) {
public function sendSpsAliasQuery($queryArgs) {
$result = $this->sqlManager->sendSpsAliasQuery($queryArgs);
return \utiliy\XmlManager::arrayToSpsXml($result);
}
@@ -117,7 +141,7 @@ class SpsApi extends Api {
* @param array $queryArgs
* @return querry result as xml
*/
public function sendSpsCoordinateQuery($queryArgs = array()) {
public function sendSpsCoordinateQuery($queryArgs) {
if (!array_key_exists(SpsApi::$keyLong, $queryArgs) || !array_key_exists(SpsApi::$keyLat, $queryArgs)) {
return null;
}

View File

@@ -0,0 +1,116 @@
<?php
namespace database;
include_once "../../global.inc.php";
include_once PATH_UTILITTY . "/StringManager.php";
require_once PATH_DATABASE . "/SqlManager.php";
/**
* Description of ZisSqlManager
*
* @author stubbfel
* @since 20.06.2013
*/
class PisSqlManager extends SQLManager {
/**
* Fieldname of the placeID
* @var string
*/
public static $placeId = "pid";
/**
* Fieldname of the name of the information
* @var string
*/
public static $infName = "iName";
/**
* Fieldname of the value of the information
* @var string
*/
public static $infValue = "iValue";
/**
* String for the select part of the query
* @var string
*/
private $selectTerm = "SELECT pid, iName, iValue FROM pis WHERE ";
/**
* String for the orderby part of the query
* @var string
*/
private $orderByTerm = " ORDER BY pid";
/**
* String for the pid part of the query
* @var string
*/
private $pidTerm = "pid = ";
/**
* Default-Constructor
*/
public function __construct() {
parent::__construct();
}
/**
* Default-DEConstructor
*/
public function __destruct() {
parent::__destruct();
}
/**
* Methods send an query for the pis-service
* @param array $queryArgs
* @return array [num][assoc]
*/
public function sendPisQuery($queryArgs) {
// build query string
$query = $this->selectTerm;
if ($this->validPidList($queryArgs)) {
$query .= $this->createPidWhereString($queryArgs) . $this->orderByTerm;
} else {
return null;
}
// send query
return $this->query($query);
}
/**
* Method check if all items of the pidlist are orly digits
* @param array $poly
* @return boolean
*/
private function validPidList($pidList) {
foreach ($pidList as $value) {
if (!ctype_digit($value) || PHP_INT_MAX < $value) {
return FALSE;
}
}
return TRUE;
}
/**
* Method convert a pidList to a where-string
* @param array $pidList
* @return string
*/
private function createPidWhereString($pidList) {
$pidListStr = \utiliy\StringManager::$emptyString;
foreach ($pidList as $value) {
$pidListStr .= $this->pidTerm . $value . $this->orTerm;
}
$result = substr($pidListStr, 0, strlen($pidListStr) - strlen($this->orTerm));
return $result;
}
}
?>

View File

@@ -105,7 +105,7 @@ class SpsSqlManager extends SQLManager {
* @param array $queryArgs
* @return array [num][assoc]
*/
public function sendSpsAliasQuery($queryArgs = array()) {
public function sendSpsAliasQuery($queryArgs) {
// check arguments of the query
if (array_key_exists(\api\SpsApi::$keyAlias, $queryArgs)) {
@@ -136,7 +136,7 @@ class SpsSqlManager extends SQLManager {
* @param array $queryArgs
* @return array [num][assoc]
*/
public function sendSpsCoordinateQuery($queryArgs = array()) {
public function sendSpsCoordinateQuery($queryArgs) {
// check arguments of the query
if (array_key_exists(\api\SpsApi::$keyPoly, $queryArgs)) {

View File

@@ -43,11 +43,17 @@ abstract class SqlManager {
private $link;
/**
* String for a and operrator
* String for an and-operrator
* @var string
*/
protected $andTerm = " and ";
/**
* String for an or-operrator
* @var string
*/
protected $orTerm = " or ";
/**
* String for quotes in a query
* @var string
@@ -69,6 +75,10 @@ abstract class SqlManager {
*/
public function __destruct() {
$this->closeConnection();
unset($this->serverAddress);
unset($this->dbName);
unset($this->userName);
unset($this->userPW);
$this->serverAddress = null;
$this->dbName = null;
$this->userName = null;
@@ -95,6 +105,7 @@ abstract class SqlManager {
public function closeConnection() {
if ($this->link) {
mysql_close($this->link);
unset($this->link);
$this->link = null;
}
}

View File

@@ -0,0 +1,2 @@
RewriteEngine on
RewriteRule ^ index.php [QSA,L]

View File

@@ -0,0 +1,14 @@
<?php
include_once "../../global.inc.php";
require_once PATH_API . "/PisApi.php";
// instance a new api
$app = new \api\PisApi();
$app->get(\api\PisApi::$routeParameterPids, function ($pid) use ($app) {
echo $app->sendPisQuery($pid);
});
$app->run();
?>

View File

@@ -7,27 +7,28 @@ require_once PATH_API . "/SpsApi.php";
$app = new \api\SpsApi();
// declare the get-methods
$app->get('/alias/:alias', function ($alias) use ($app) {
// declare the get-methods
$app->get(\api\SpsApi::$routeParameterAlias, function ($alias) use ($app) {
$args = array();
$args[\api\SpsApi::$keyAlias] = $alias;
echo $app->sendSpsAliasQuery($args);
});
$app->get('/alias/:alias/domain/:domain', function ($alias, $domain) use ($app) {
$app->get(\api\SpsApi::$routeParameterAlias . \api\SpsApi::$routeParameterDomain, function ($alias, $domain) use ($app) {
$args = array();
$args[\api\SpsApi::$keyAlias] = $alias;
$args[\api\SpsApi::$keyDomain] = $domain;
echo $app->sendSpsAliasQuery($args);
});
$app->get('/longitude/:longitude/latitude/:latitude', function ($longitude, $latitude) use ($app) {
$app->get(\api\SpsApi::$routeParameterLongitude . \api\SpsApi::$routeParameterLatitude, function ($longitude, $latitude) use ($app) {
$args = array();
$args[\api\SpsApi::$keyLong] = $longitude;
$args[\api\SpsApi::$keyLat] = $latitude;
echo ($app->sendSpsCoordinateQuery($args));
});
$app->get('/longitude/:longitude/latitude/:latitude/domain/:domain', function ($longitude, $latitude, $domain) use ($app) {
$app->get(\api\SpsApi::$routeParameterLongitude . \api\SpsApi::$routeParameterLatitude . \api\SpsApi::$routeParameterDomain, function ($longitude, $latitude, $domain) use ($app) {
$args = array();
$args[\api\SpsApi::$keyLong] = $longitude;
$args[\api\SpsApi::$keyLat] = $latitude;

View File

@@ -10,6 +10,13 @@ include_once "../../global.inc.php";
*/
class StringManager {
/**
* A Constant vor an emptystring like \"\"
* @var string
*/
public static $emptyString = "";
/**
* Method check if a certain string start with a certain sustring
* @param string $haystack

View File

@@ -23,6 +23,24 @@ class XmlManager {
*/
public static $placeElementName = "place";
/**
* Name for the placeinformation element
* @var string
*/
public static $placeInfoElementName = "placeInformation";
/**
* Name for the placeInfoName attribute
* @var string
*/
public static $placeInfoName = "placeInformationName";
/**
* Name for the place element
* @var string
*/
public static $placeInfoValue = "placeInformationValue";
/**
* Name for the placeid attribute
* @var string
@@ -51,6 +69,34 @@ class XmlManager {
return $xml->asXML();
}
/**
* Method convert an array to a response xml for the sps service
* @param array[num][assoc] $result
* @return xml-string
*/
public static function arrayToPisXml($result = array()) {
$xml = new \SimpleXMLElement(XmlManager::$defaultXmlDoc);
$actPlace = 0;
foreach ($result as $row) {
// fetch the place id of the row
$placeId = $row[\database\PisSqlManager::$placeId];
// if the id is new -> add new place element
if ($actPlace != $placeId) {
$actPlace = $placeId;
$place = $xml->addChild(XmlManager::$placeElementName);
$place->addAttribute(XmlManager::$placeIdAttrName, $placeId);
}
// add placeinformation elment
$placeInfo = $place->addChild(XmlManager::$placeInfoElementName, utf8_encode($row[\database\PisSqlManager::$infValue]));
$placeInfo->addAttribute(XmlManager::$placeInfoName, $row[\database\PisSqlManager::$infName]);
}
return $xml->asXML();
}
}
?>