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\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  }