|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00050 class Net_GeoIP_Location implements Serializable 00051 { 00052 protected $aData = array( 00053 'countryCode' => null, 00054 'countryCode3' => null, 00055 'countryName' => null, 00056 'region' => null, 00057 'city' => null, 00058 'postalCode' => null, 00059 'latitude' => null, 00060 'longitude' => null, 00061 'areaCode' => null, 00062 'dmaCode' => null 00063 ); 00064 00065 00073 public function distance(Net_GeoIP_Location $loc) 00074 { 00075 // ideally these should be class constants, but class constants 00076 // can't be operations. 00077 $RAD_CONVERT = M_PI / 180; 00078 $EARTH_DIAMETER = 2 * 6378.2; 00079 00080 $lat1 = $this->latitude; 00081 $lon1 = $this->longitude; 00082 $lat2 = $loc->latitude; 00083 $lon2 = $loc->longitude; 00084 00085 // convert degrees to radians 00086 $lat1 *= $RAD_CONVERT; 00087 $lat2 *= $RAD_CONVERT; 00088 00089 // find the deltas 00090 $delta_lat = $lat2 - $lat1; 00091 $delta_lon = ($lon2 - $lon1) * $RAD_CONVERT; 00092 00093 // Find the great circle distance 00094 $temp = pow(sin($delta_lat/2), 2) + cos($lat1) * cos($lat2) * pow(sin($delta_lon/2), 2); 00095 return $EARTH_DIAMETER * atan2(sqrt($temp), sqrt(1-$temp)); 00096 } 00097 00107 public function serialize() 00108 { 00109 return serialize($this->aData); 00110 } 00111 00119 public function unserialize($serialized) 00120 { 00121 $this->aData = unserialize($serialized); 00122 } 00123 00124 00133 public function set($name, $val) 00134 { 00135 if (array_key_exists($name, $this->aData)) { 00136 $this->aData[$name] = $val; 00137 } 00138 00139 return $this; 00140 } 00141 00142 public function __set($name, $val) 00143 { 00144 return $this->set($name, $val); 00145 } 00146 00152 public function getData() 00153 { 00154 return $this->aData; 00155 } 00156 00157 00166 public function __get($name) 00167 { 00168 if (array_key_exists($name, $this->aData)) { 00169 return $this->aData[$name]; 00170 } 00171 00172 return null; 00173 } 00174 00175 00181 public function __toString() 00182 { 00183 return 'object of type '.__CLASS__.'. data: '.implode(',', $this->aData); 00184 } 00185 00186 00196 public function __isset($name) 00197 { 00198 return (null !== $this->__get($name)); 00199 } 00200 00201 }