Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

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