1: <?php
2:
3: namespace api;
4:
5: include_once "../../global.inc.php";
6: include_once PATH_DATABASE . "/SpsSqlManager.php";
7: include_once PATH_UTILITTY . "/XmlManager.php";
8: require_once PATH_API . "/Api.php";
9:
10: 11: 12: 13: 14:
15: class SpsApi extends Api {
16:
17: 18: 19: 20:
21: public static $routeParameterAlias = "/alias/:alias";
22:
23: 24: 25: 26:
27: public static $routeParameterDomain = "/domain/:domain";
28:
29: 30: 31: 32:
33: public static $routeParameterLongitude = "/longitude/:longitude";
34:
35: 36: 37: 38:
39: public static $routeParameterLatitude = "/latitude/:latitude";
40:
41: 42: 43: 44:
45: public static $keyAlias = "alias";
46:
47: 48: 49: 50:
51: public static $keyDomain = "domain";
52:
53: 54: 55: 56:
57: public static $keyPoly = "poly";
58:
59: 60: 61: 62:
63: public static $keyLong = "longitude";
64:
65: 66: 67: 68:
69: public static $keyLat = "latitude";
70:
71: 72: 73: 74:
75: private $range = 1;
76:
77: 78: 79: 80:
81: private $polyStartStr = "GeomFromText('Polygon((";
82:
83: 84: 85: 86:
87: private $polyEndStr = "))'";
88:
89: 90: 91: 92:
93: private $maxLat = 180;
94:
95: 96: 97: 98:
99: private $minLat = -180;
100:
101: 102: 103: 104:
105: private $maxLong = 180;
106:
107: 108: 109: 110:
111: private $minLong = -180;
112:
113: 114: 115:
116: public function __construct() {
117: $this->sqlManager = new \database\SpsSqlManager();
118: parent::__construct();
119: }
120:
121: 122: 123:
124: public function __destruct() {
125: parent::__destruct();
126: }
127:
128: 129: 130: 131: 132:
133: public function sendSpsAliasQuery($queryArgs) {
134: $result = $this->sqlManager->sendSpsAliasQuery($queryArgs);
135: return \utiliy\XmlManager::arrayToSpsXml($result);
136: }
137:
138: 139: 140: 141: 142:
143: public function sendSpsCoordinateQuery($queryArgs) {
144:
145:
146: if (!array_key_exists(SpsApi::$keyLong, $queryArgs) || !array_key_exists(SpsApi::$keyLat, $queryArgs)) {
147: return null;
148: }
149: $latitude = $queryArgs[SpsApi::$keyLat];
150: $longitude = $queryArgs[SpsApi::$keyLong];
151:
152: if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) {
153: return null;
154: }
155:
156:
157: $queryArgs[SpsApi::$keyPoly] = $this->createPolygon($latitude, $longitude, $this->range);
158:
159:
160: $result = $this->sqlManager->sendSpsCoordinateQuery($queryArgs);
161: return \utiliy\XmlManager::arrayToSpsXml($result);
162: }
163:
164: 165: 166: 167: 168:
169: private function validLongitude($string) {
170: $digitLessPoint = str_replace(".", "", $string);
171: $digit = str_replace("-", "", $digitLessPoint);
172: if (ctype_digit($digit)) {
173: if ($string <= $this->maxLong && $string >= $this->minLong) {
174: return TRUE;
175: }
176: };
177: return FALSE;
178: }
179:
180: 181: 182: 183: 184:
185: private function validLatitude($string) {
186: $digitLessPoint = str_replace(".", "", $string);
187: $digit = str_replace("-", "", $digitLessPoint);
188: if (ctype_digit($digit)) {
189: if ($string <= $this->maxLat && $string >= $this->minLat) {
190: return TRUE;
191: }
192: };
193: return FALSE;
194: }
195:
196: 197: 198: 199: 200: 201: 202:
203: private function createPolygon($latitude, $longitude, $range) {
204: $minLat = $latitude - $range;
205: $minLong = $longitude - $range;
206: $maxLat = $latitude + $range;
207: $maxLong = $longitude + $range;
208: return $this->polyStartStr . "$minLat $minLong,$minLat $maxLong,$maxLat $maxLong,$maxLat $minLong,$minLat $minLong" . $this->polyEndStr;
209: }
210:
211: }
212:
213: ?>
214: