Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace GeoIp2\Record;
   6  
   7  abstract class AbstractRecord implements \JsonSerializable
   8  {
   9      private $record;
  10  
  11      /**
  12       * @ignore
  13       */
  14      public function __construct(?array $record)
  15      {
  16          $this->record = isset($record) ? $record : [];
  17      }
  18  
  19      /**
  20       * @ignore
  21       */
  22      public function __get(string $attr)
  23      {
  24          // XXX - kind of ugly but greatly reduces boilerplate code
  25          $key = $this->attributeToKey($attr);
  26  
  27          if ($this->__isset($attr)) {
  28              return $this->record[$key];
  29          } elseif ($this->validAttribute($attr)) {
  30              if (preg_match('/^is_/', $key)) {
  31                  return false;
  32              }
  33  
  34              return null;
  35          }
  36          throw new \RuntimeException("Unknown attribute: $attr");
  37      }
  38  
  39      public function __isset(string $attr): bool
  40      {
  41          return $this->validAttribute($attr) &&
  42               isset($this->record[$this->attributeToKey($attr)]);
  43      }
  44  
  45      private function attributeToKey(string $attr): string
  46      {
  47          return strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
  48      }
  49  
  50      private function validAttribute(string $attr): bool
  51      {
  52          return \in_array($attr, $this->validAttributes, true);
  53      }
  54  
  55      public function jsonSerialize(): ?array
  56      {
  57          return $this->record;
  58      }
  59  }