PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/geocoder/GeoCoder.php

https://bitbucket.org/ddonthula/zurmozoo-1.0
PHP | 298 lines | 125 code | 32 blank | 141 comment | 13 complexity | e74882ea4fe2909ad948b3ed60836f17 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-3.0
  1. <?php
  2. /**
  3. * Copyright (c) 2009 Brian Armstrong
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * Class to handle GeoCode requests
  25. *
  26. * @package GeoCoder
  27. * @author Brian Armstrong <brian@barmstrongconsulting.com>
  28. * @copyright (C) 2009 Brian Armstrong
  29. * @link http://barmstrongconsulting.com/
  30. * @version 1.0
  31. */
  32. class GeoCoder extends CApplicationComponent
  33. {
  34. /**
  35. * The name of the API driver to use for this GeoCoder
  36. * Available Drivers: google,yahoo
  37. * @var string
  38. */
  39. protected $api_driver = null;
  40. /**
  41. * The API key to use for our queries
  42. * See your driver for more information about obtaining an API key
  43. * @var string
  44. */
  45. protected $api_key = null;
  46. /**
  47. * The variable to hold the last query string that we used
  48. * @var string
  49. */
  50. protected $query_str = null;
  51. /**
  52. * The variable to hold the instance of our GeoCode_Parser
  53. * @var GeoCode_Driver
  54. */
  55. private $_driver = null;
  56. /**
  57. * Handle the autoloading functionality for this extension
  58. * @param mixed $class_name
  59. */
  60. public static function autoload($class_name)
  61. {
  62. if (class_exists($class_name, false))
  63. return true;
  64. // If our class starts with GeoCode_, it is ours
  65. if (preg_match('/^geocode_/i', $class_name))
  66. {
  67. if ($path = self::getClassPath($class_name))
  68. {
  69. // Include the file
  70. require_once($path);
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * Get the path to a class file inside our system
  78. * @param string $class
  79. * @return mixed the path or FALSE on failure
  80. */
  81. protected static function getClassPath($class)
  82. {
  83. // Variable to store our path string
  84. $path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  85. // Split out the parts
  86. $parts = explode('_', strtolower($class));
  87. // Get the file part
  88. $file = array_pop($parts);
  89. // Process each part of the string for it's directory equivalent
  90. foreach ($parts as $part)
  91. {
  92. // Custom handling. This is for special cases
  93. // that would make good looking class names
  94. // line up with a good looking direcotry structure.
  95. switch ($part)
  96. {
  97. case 'geocode':
  98. $part = 'libraries';
  99. break;
  100. case 'driver':
  101. $part = 'drivers';
  102. break;
  103. }
  104. // Append the path
  105. $path .= $part . DIRECTORY_SEPARATOR;
  106. }
  107. // Append the file name and extension
  108. $path .= ucfirst($file) . '.php';
  109. // Return the path
  110. return (file_exists($path)) ? $path : false;
  111. }
  112. /**
  113. * Initialize the plugin
  114. */
  115. public function init()
  116. {
  117. // Only init once
  118. if (!$this->getIsInitialized())
  119. {
  120. // Register the autoloader
  121. Yii::registerAutoloader(array('GeoCoder', 'autoload'));
  122. // Initialize the driver
  123. $this->setDriver($this->api_driver);
  124. // Cascade
  125. parent::init();
  126. }
  127. }
  128. /**
  129. * Send a query to the geocode API
  130. * See {@link GeoCode_Parser::process} for more information on the return value
  131. * @param mixed $query
  132. * @return GeoCode_Result
  133. */
  134. public function query($query)
  135. {
  136. // If we don't have a valid driver, exit
  137. if ($this->_driver === null)
  138. throw new GeoCode_Exception("No GeoCode Driver Specified");
  139. return $this->_driver->query($query);
  140. }
  141. /**
  142. * Change the api driver string
  143. * @param string $driver
  144. */
  145. public function setApiDriver($driver)
  146. {
  147. // If we are initialized, set the driver as well
  148. if ($this->getIsInitialized())
  149. {
  150. $this->setDriver($driver);
  151. }
  152. // Otherwise, just set the string that will be used
  153. // when we initialize the component
  154. else
  155. {
  156. $this->api_driver = $driver;
  157. }
  158. }
  159. /**
  160. * Get the api driver string
  161. * @return string
  162. */
  163. public function getApiDriver()
  164. {
  165. return $this->api_driver;
  166. }
  167. /**
  168. * Set the API key for this system
  169. * @param mixed $key
  170. */
  171. public function setApiKey($key)
  172. {
  173. $this->api_key = $key;
  174. // Save it to the driver as well
  175. if ($this->_driver !== null)
  176. $this->_driver->setApiKey($this->api_key);
  177. }
  178. /**
  179. * Get the api key
  180. * @return string
  181. */
  182. public function getApiKey()
  183. {
  184. return $this->api_key;
  185. }
  186. /**
  187. * Set the driver for the GeoCode requests
  188. * @param string $driver
  189. * @return boolean
  190. */
  191. public function setDriver($driver)
  192. {
  193. // Lowercase for comparisons
  194. $driver = strtolower($driver);
  195. // If this driver is the same as the current driver, skip
  196. if ($this->_driver !== null && $this->api_driver == $driver)
  197. return true;
  198. // Make sure the driver exists
  199. if (!empty($driver))
  200. {
  201. // Load the new driver
  202. $this->_driver = GeoCode_Driver::factory($driver, $this->api_key);
  203. // Save the driver string
  204. $this->api_driver = $driver;
  205. return true;
  206. }
  207. return false;
  208. }
  209. /**
  210. * Magic call method. If the method is not accessible in the class,
  211. * then we check if it exists on the driver.
  212. * @param string $name
  213. * @param array $parameters
  214. * @return mixed
  215. */
  216. public function __call($name, $parameters)
  217. {
  218. if (method_exists($this->_driver, $name))
  219. {
  220. return call_user_func_array(array($this->_driver, $name), $parameters);
  221. }
  222. parent::__call($name, $parameters);
  223. }
  224. /**
  225. * Magic get method. If a get<Name> method exists, then it is called.
  226. * Otherwise, sent to parent
  227. * @param string $name
  228. * @return mixed
  229. */
  230. public function __get($name)
  231. {
  232. $name = str_replace('_', '', $name);
  233. return parent::__get($name);
  234. }
  235. /**
  236. * Magic set method. If a set<Name> method exists, then it is called.
  237. * Otherwise, pass to the parent
  238. * @param string $name
  239. * @param mixed $value
  240. */
  241. public function __set($name, $value)
  242. {
  243. $name = str_replace('_', '', $name);
  244. parent::__set($name, $value);
  245. }
  246. /**
  247. * Magic isset function. Checks if an attribute is set
  248. * @param string $name
  249. * @return boolean
  250. */
  251. public function __isset($name)
  252. {
  253. $name = str_replace('_', '', $name);
  254. return parent::__isset($name);
  255. }
  256. /**
  257. * Magic unset function. Removes a given attribute
  258. * @param string $name
  259. */
  260. public function __unset($name)
  261. {
  262. $name = str_replace('_', '', $name);
  263. parent::__unset($name);
  264. }
  265. }
  266. ?>