diff --git a/geoapi/api/PisApi.php b/geoapi/api/PisApi.php new file mode 100644 index 0000000..0a8fb94 --- /dev/null +++ b/geoapi/api/PisApi.php @@ -0,0 +1,185 @@ +sqlManager = new \database\SpsSqlManager(); + parent::__construct(); + $this->contentType("Content-type: application/xml;charset=utf-8"); + } + + /** + * Default-DeConstructor + */ + public function __destruct() { + parent::__destruct(); + } + + /** + * Method start a sps-query(alias) + * @param array $queryArgs + * @return querry result as xml + */ + public function sendSpsAliasQuery($queryArgs = array()) { + $result = $this->sqlManager->sendSpsAliasQuery($queryArgs); + return \utiliy\XmlManager::arrayToSpsXml($result); + } + + /** + * Method start a sps-query(Coordinates) + * @param array $queryArgs + * @return querry result as xml + */ + public function sendSpsCoordinateQuery($queryArgs = array()) { + if (!array_key_exists(SpsApi::$keyLong, $queryArgs) || !array_key_exists(SpsApi::$keyLat, $queryArgs)) { + return null; + } + $latitude = $queryArgs[SpsApi::$keyLat]; + $longitude = $queryArgs[SpsApi::$keyLong]; + + if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) { + return null; + } + + $queryArgs[SpsApi::$keyPoly] = $this->createPolygon($latitude, $longitude, $this->range); + $result = $this->sqlManager->sendSpsCoordinateQuery($queryArgs); + return \utiliy\XmlManager::arrayToSpsXml($result); + } + + /** + * Method check if a string is a valid Longitude + * @param string $string + * @return bool + */ + private function validLongitude($string) { + $digitLessPoint = str_replace(".", "", $string); + $digit = str_replace("-", "", $digitLessPoint); + if (ctype_digit($digit)) { + if ($string <= $this->maxLong && $string >= $this->minLong) { + return TRUE; + } + }; + return FALSE; + } + + /** + * Method check if a string is a valid Latitude + * @param strinf $string + * @return bool + */ + private function validLatitude($string) { + $digitLessPoint = str_replace(".", "", $string); + $digit = str_replace("-", "", $digitLessPoint); + if (ctype_digit($digit)) { + if ($string <= $this->maxLat && $string >= $this->minLat) { + return TRUE; + } + }; + return FALSE; + } + + /** + * Method create a Polygon + * @param float $latitude + * @param float $longitude + * @param float $range + * @return string + */ + private function createPolygon($latitude, $longitude, $range) { + $minLat = $latitude - $range; + $minLong = $longitude - $range; + $maxLat = $latitude + $range; + $maxLong = $longitude + $range; + return $this->polyStartStr . "$minLat $minLong,$minLat $maxLong,$maxLat $maxLong,$maxLat $minLong,$minLat $minLong" . $this->polyEndStr; + } + +} + +?> diff --git a/geoapi/database/PisSqlManager.php b/geoapi/database/PisSqlManager.php new file mode 100644 index 0000000..1019aa3 --- /dev/null +++ b/geoapi/database/PisSqlManager.php @@ -0,0 +1,229 @@ +selectTerm; + if ($this->validAliasString($alias)) { + $query .= $this->aliasTerm . $this->quoteTerm . $alias . $this->quoteTerm . $this->addDomainTerm($domain); + } else { + return null; + } + + // send query + return $this->query($query); + } + + /** + * Methods send an query for the sps-service depends of coordinates + * @param array $queryArgs + * @return array [num][assoc] + */ + public function sendSpsCoordinateQuery($queryArgs = array()) { + + // check arguments of the query + if (array_key_exists(\api\SpsApi::$keyPoly, $queryArgs)) { + $poly = $queryArgs[\api\SpsApi::$keyPoly]; + } else { + return null; + } + + $domain = null; + if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) { + $domain = $queryArgs[\api\SpsApi::$keyDomain]; + } + + // build query string + if ($this->validPolyString($poly)) { + $query = $this->selectTerm . $this->interSectTermStart . $poly . $this->interSectTermEnd . $this->addDomainTerm($domain); + } else { + return null; + } + + // send query + return $this->query($query); + } + + /** + * Method create the correct domain part depends of $domain. If it is a number => did + * otherwise => dName + * @param string $domain + * @return string + */ + private function addDomainTerm($domain) { + $result = null; + if ($domain != null && $this->validDomainString($domain)) { + if ($this->isDid($domain)) { + $result .= $this->andTerm . $this->domainTerm . $this->quoteTerm . $domain . $this->quoteTerm; + } else { + $result .= $this->andTerm . $this->domainNameTerm . $this->quoteTerm . $domain . $this->quoteTerm; + } + } + return $result; + } + + /** + * Method check if the input value for the alias is valid + * @param string $alias + * @return boolean + */ + private function validAliasString($alias) { + if ($this->validString($alias) && ctype_alnum($alias) && strlen($alias) <= $this->aliasMaxLenght) { + return TRUE; + } + return FALSE; + } + + /** + * Method check if the input value for the alias is valid + * @param string $domain + * @return boolean + */ + private function validDomainString($domain) { + if ($this->validString($domain) && ctype_alnum($domain) && strlen($domain) <= $this->domainMaxLenght) { + return TRUE; + } + return FALSE; + } + + /** + * Method check if the input value for the polygon is valid + * @param string $poly + * @return boolean + */ + private function validPolyString($poly) { + if ($this->validString($poly) && \utiliy\StringManager::startsWith($poly, $this->polyStartStr) && \utiliy\StringManager::endsWith($poly, $this->polyEndStr)) { + return TRUE; + } + return FALSE; + } + + /** + * Method check if the ntring is a did + * @param string $string + * @return boolean + */ + private function isDid($string) { + return ctype_digit($string); + } + +} + +?> diff --git a/geoapi/service/pis/.htaccess b/geoapi/service/pis/.htaccess new file mode 100644 index 0000000..450c0d1 --- /dev/null +++ b/geoapi/service/pis/.htaccess @@ -0,0 +1,2 @@ +RewriteEngine on +RewriteRule ^ index.php [QSA,L] \ No newline at end of file diff --git a/geoapi/service/pis/index.php b/geoapi/service/pis/index.php new file mode 100644 index 0000000..21c139f --- /dev/null +++ b/geoapi/service/pis/index.php @@ -0,0 +1,39 @@ +get('/alias/:alias', 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) { + $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) { + $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) { + $args = array(); + $args[\api\SpsApi::$keyLong] = $longitude; + $args[\api\SpsApi::$keyLat] = $latitude; + $args[\api\SpsApi::$keyDomain] = $domain; + echo ($app->sendSpsCoordinateQuery($args)); + }); + +$app->run(); +?>