[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/pear/Net/GeoIP/ -> Location.php (source)

   1  <?php
   2  /**
   3   * +----------------------------------------------------------------------+
   4   * | PHP version 5                                                        |
   5   * +----------------------------------------------------------------------+
   6   * | Copyright (C) 2004 MaxMind LLC                                       |
   7   * +----------------------------------------------------------------------+
   8   * | This library is free software; you can redistribute it and/or        |
   9   * | modify it under the terms of the GNU Lesser General Public           |
  10   * | License as published by the Free Software Foundation; either         |
  11   * | version 2.1 of the License, or (at your option) any later version.   |
  12   * |                                                                      |
  13   * | This library is distributed in the hope that it will be useful,      |
  14   * | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15   * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  16   * | Lesser General Public License for more details.                      |
  17   * |                                                                      |
  18   * | You should have received a copy of the GNU Lesser General Public     |
  19   * | License along with this library; if not, write to the Free Software  |
  20   * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 |
  21   * | USA, or view it online at http://www.gnu.org/licenses/lgpl.txt.      |
  22   * +----------------------------------------------------------------------+
  23   * | Authors: Jim Winstead <jimw@apache.org> (original Maxmind version)   |
  24   * |          Hans Lellelid <hans@xmpl.org>                               |
  25   * +----------------------------------------------------------------------+
  26   *
  27   * @category Net
  28   * @package  Net_GeoIP
  29   * @author   Hans Lellelid <hans@xmpl.org>
  30   * @license  LGPL http://www.gnu.org/licenses/lgpl.txt
  31   * @link     http://pear.php.net/package/Net_GeoIp
  32   * $Id$
  33   */
  34  
  35  /**
  36   * This class represents a location record as returned by Net_GeoIP::lookupLocation().
  37   *
  38   * This class is primarily a collection of values (the public properties of the class), but
  39   * there is also a distance() method to calculate the km distance between two points.
  40   *
  41   * @category Net
  42   * @package  Net_GeoIP
  43   * @author   Hans Lellelid <hans@xmpl.org>
  44   * @author   Dmitri Snytkine <d.snytkine@gmail.com>
  45   * @license  LGPL http://www.gnu.org/licenses/lgpl.txt
  46   * @version  $Revision$
  47   * @link     http://pear.php.net/package/Net_GeoIp
  48   * @see      Net_GeoIP::lookupLocation()
  49   */
  50  class Net_GeoIP_Location implements Serializable
  51  {
  52      protected $aData = array(
  53          'countryCode'  => null,
  54          'countryCode3' => null,
  55          'countryName'  => null,
  56          'region'       => null,
  57          'city'         => null,
  58          'postalCode'   => null,
  59          'latitude'     => null,
  60          'longitude'    => null,
  61          'areaCode'     => null,
  62          'dmaCode'      => null
  63      );
  64  
  65  
  66      /**
  67       * Calculate the distance in km between two points.
  68       *
  69       * @param Net_GeoIP_Location $loc The other point to which distance will be calculated.
  70       *
  71       * @return float The number of km between two points on the globe.
  72       */
  73      public function distance(Net_GeoIP_Location $loc)
  74      {
  75          // ideally these should be class constants, but class constants
  76          // can't be operations.
  77          $RAD_CONVERT = M_PI / 180;
  78          $EARTH_DIAMETER = 2 * 6378.2;
  79  
  80          $lat1 = $this->latitude;
  81          $lon1 = $this->longitude;
  82          $lat2 = $loc->latitude;
  83          $lon2 = $loc->longitude;
  84  
  85          // convert degrees to radians
  86          $lat1 *= $RAD_CONVERT;
  87          $lat2 *= $RAD_CONVERT;
  88  
  89          // find the deltas
  90          $delta_lat = $lat2 - $lat1;
  91          $delta_lon = ($lon2 - $lon1) * $RAD_CONVERT;
  92  
  93          // Find the great circle distance
  94          $temp = pow(sin($delta_lat/2), 2) + cos($lat1) * cos($lat2) * pow(sin($delta_lon/2), 2);
  95          return $EARTH_DIAMETER * atan2(sqrt($temp), sqrt(1-$temp));
  96      }
  97  
  98      /**
  99       * magic method to make it possible
 100       * to store this object in cache when
 101       * automatic serialization is on
 102       * Specifically it makes it possible to store
 103       * this object in memcache
 104       *
 105       * @return array
 106       */
 107      public function serialize()
 108      {
 109          return serialize($this->aData);
 110      }
 111  
 112      /**
 113       * unserialize a representation of the object
 114       *
 115       * @param array $serialized The serialized representation of the location
 116       *
 117       * @return void
 118       */
 119      public function unserialize($serialized)
 120      {
 121          $this->aData = unserialize($serialized);
 122      }
 123  
 124  
 125      /**
 126       * Setter for elements of $this->aData array
 127       *
 128       * @param string $name The variable to set
 129       * @param string $val  The value
 130       *
 131       * @return object $this object
 132       */
 133      public function set($name, $val)
 134      {
 135          if (array_key_exists($name, $this->aData)) {
 136              $this->aData[$name] = $val;
 137          }
 138  
 139          return $this;
 140      }
 141  
 142      public function __set($name, $val)
 143      {
 144          return $this->set($name, $val);
 145      }
 146  
 147      /**
 148       * Getter for $this->aData array
 149       *
 150       * @return array
 151       */
 152      public function getData()
 153      {
 154           return $this->aData;
 155      }
 156  
 157  
 158      /**
 159       * Magic method to get value from $this->aData array
 160       *
 161       * @param string $name The var to get
 162       *
 163       * @return mixed string if value exists or null if it is empty of
 164       * just does not exist
 165       */
 166      public function __get($name)
 167      {
 168          if (array_key_exists($name, $this->aData)) {
 169              return $this->aData[$name];
 170          }
 171  
 172          return null;
 173      }
 174  
 175  
 176      /**
 177       * String representation of the object
 178       *
 179       * @return string text and result of print_r of $this->aData array
 180       */
 181      public function __toString()
 182      {
 183          return 'object of type '.__CLASS__.'. data: '.implode(',', $this->aData);
 184      }
 185  
 186  
 187      /**
 188       * Magic method
 189       * makes it possible to check if specific record exists
 190       * and also makes it possible to use empty() on any property
 191       *
 192       * @param strign $name The name of the var to check
 193       *
 194       * @return bool
 195       */
 196      public function __isset($name)
 197      {
 198          return (null !== $this->__get($name));
 199      }
 200  
 201  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1