Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  namespace GeoIp2\Model;
   4  
   5  /**
   6   * Model class for the data returned by GeoIP2 City web service and database.
   7   *
   8   * The only difference between the City and Insights model classes is which
   9   * fields in each record may be populated. See
  10   * https://dev.maxmind.com/geoip/geoip2/web-services for more details.
  11   *
  12   * @property-read \GeoIp2\Record\City $city City data for the requested IP
  13   * address.
  14   * @property-read \GeoIp2\Record\Location $location Location data for the
  15   * requested IP address.
  16   * @property-read \GeoIp2\Record\Postal $postal Postal data for the
  17   * requested IP address.
  18   * @property-read array $subdivisions An array \GeoIp2\Record\Subdivision
  19   * objects representing the country subdivisions for the requested IP
  20   * address. The number and type of subdivisions varies by country, but a
  21   * subdivision is typically a state, province, county, etc. Subdivisions
  22   * are ordered from most general (largest) to most specific (smallest).
  23   * If the response did not contain any subdivisions, this method returns
  24   * an empty array.
  25   * @property-read \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
  26   * representing the most specific subdivision returned. If the response
  27   * did not contain any subdivisions, this method returns an empty
  28   * \GeoIp2\Record\Subdivision object.
  29   */
  30  class City extends Country
  31  {
  32      /**
  33       * @ignore
  34       */
  35      protected $city;
  36      /**
  37       * @ignore
  38       */
  39      protected $location;
  40      /**
  41       * @ignore
  42       */
  43      protected $postal;
  44      /**
  45       * @ignore
  46       */
  47      protected $subdivisions = [];
  48  
  49      /**
  50       * @ignore
  51       *
  52       * @param mixed $raw
  53       * @param mixed $locales
  54       */
  55      public function __construct($raw, $locales = ['en'])
  56      {
  57          parent::__construct($raw, $locales);
  58  
  59          $this->city = new \GeoIp2\Record\City($this->get('city'), $locales);
  60          $this->location = new \GeoIp2\Record\Location($this->get('location'));
  61          $this->postal = new \GeoIp2\Record\Postal($this->get('postal'));
  62  
  63          $this->createSubdivisions($raw, $locales);
  64      }
  65  
  66      private function createSubdivisions($raw, $locales)
  67      {
  68          if (!isset($raw['subdivisions'])) {
  69              return;
  70          }
  71  
  72          foreach ($raw['subdivisions'] as $sub) {
  73              array_push(
  74                  $this->subdivisions,
  75                  new \GeoIp2\Record\Subdivision($sub, $locales)
  76              );
  77          }
  78      }
  79  
  80      /**
  81       * @ignore
  82       *
  83       * @param mixed $attr
  84       */
  85      public function __get($attr)
  86      {
  87          if ($attr === 'mostSpecificSubdivision') {
  88              return $this->$attr();
  89          }
  90  
  91          return parent::__get($attr);
  92      }
  93  
  94      /**
  95       * @ignore
  96       *
  97       * @param mixed $attr
  98       */
  99      public function __isset($attr)
 100      {
 101          if ($attr === 'mostSpecificSubdivision') {
 102              // We always return a mostSpecificSubdivision, even if it is the
 103              // empty subdivision
 104              return true;
 105          }
 106  
 107          return parent::__isset($attr);
 108      }
 109  
 110      private function mostSpecificSubdivision()
 111      {
 112          return empty($this->subdivisions) ?
 113              new \GeoIp2\Record\Subdivision([], $this->locales) :
 114              end($this->subdivisions);
 115      }
 116  }