custom/plugins/AcrisShopSwitchCS/src/Resources/GeoIp2/vendor/geoip2/geoip2/src/Database/Reader.php line 86

Open in your IDE?
  1. <?php
  2. namespace GeoIp2\Database;
  3. use GeoIp2\Exception\AddressNotFoundException;
  4. use GeoIp2\ProviderInterface;
  5. use MaxMind\Db\Reader as DbReader;
  6. use MaxMind\Db\Reader\InvalidDatabaseException;
  7. /**
  8.  * Instances of this class provide a reader for the GeoIP2 database format.
  9.  * IP addresses can be looked up using the database specific methods.
  10.  *
  11.  * ## Usage ##
  12.  *
  13.  * The basic API for this class is the same for every database. First, you
  14.  * create a reader object, specifying a file name. You then call the method
  15.  * corresponding to the specific database, passing it the IP address you want
  16.  * to look up.
  17.  *
  18.  * If the request succeeds, the method call will return a model class for
  19.  * the method you called. This model in turn contains multiple record classes,
  20.  * each of which represents part of the data returned by the database. If
  21.  * the database does not contain the requested information, the attributes
  22.  * on the record class will have a `null` value.
  23.  *
  24.  * If the address is not in the database, an
  25.  * {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
  26.  * thrown. If an invalid IP address is passed to one of the methods, a
  27.  * SPL {@link \InvalidArgumentException} will be thrown. If the database is
  28.  * corrupt or invalid, a {@link \MaxMind\Db\Reader\InvalidDatabaseException}
  29.  * will be thrown.
  30.  *
  31.  */
  32. class Reader implements ProviderInterface
  33. {
  34.     private $dbReader;
  35.     private $locales;
  36.     /**
  37.      * Constructor.
  38.      *
  39.      * @param string $filename The path to the GeoIP2 database file.
  40.      * @param array $locales  List of locale codes to use in name property
  41.      * from most preferred to least preferred.
  42.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  43.      *          is corrupt or invalid
  44.      */
  45.     public function __construct(
  46.         $filename,
  47.         $locales = array('en')
  48.     ) {
  49.         $this->dbReader = new DbReader($filename);
  50.         $this->locales $locales;
  51.     }
  52.     /**
  53.      * This method returns a GeoIP2 City model.
  54.      *
  55.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  56.      *
  57.      * @return \GeoIp2\Model\City
  58.      *
  59.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  60.      *         not in the database.
  61.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  62.      *         is corrupt or invalid
  63.      */
  64.     public function city($ipAddress)
  65.     {
  66.         return $this->modelFor('City''City'$ipAddress);
  67.     }
  68.     /**
  69.      * This method returns a GeoIP2 Country model.
  70.      *
  71.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  72.      *
  73.      * @return \GeoIp2\Model\Country
  74.      *
  75.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  76.      *         not in the database.
  77.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  78.      *         is corrupt or invalid
  79.      */
  80.     public function country($ipAddress)
  81.     {
  82.         return $this->modelFor('Country''Country'$ipAddress);
  83.     }
  84.     /**
  85.      * This method returns a GeoIP2 Anonymous IP model.
  86.      *
  87.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  88.      *
  89.      * @return \GeoIp2\Model\AnonymousIp
  90.      *
  91.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  92.      *         not in the database.
  93.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  94.      *         is corrupt or invalid
  95.      */
  96.     public function anonymousIp($ipAddress)
  97.     {
  98.         return $this->flatModelFor(
  99.             'AnonymousIp',
  100.             'GeoIP2-Anonymous-IP',
  101.             $ipAddress
  102.         );
  103.     }
  104.     /**
  105.      * This method returns a GeoIP2 Connection Type model.
  106.      *
  107.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  108.      *
  109.      * @return \GeoIp2\Model\ConnectionType
  110.      *
  111.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  112.      *         not in the database.
  113.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  114.      *         is corrupt or invalid
  115.      */
  116.     public function connectionType($ipAddress)
  117.     {
  118.         return $this->flatModelFor(
  119.             'ConnectionType',
  120.             'GeoIP2-Connection-Type',
  121.             $ipAddress
  122.         );
  123.     }
  124.     /**
  125.      * This method returns a GeoIP2 Domain model.
  126.      *
  127.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  128.      *
  129.      * @return \GeoIp2\Model\Domain
  130.      *
  131.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  132.      *         not in the database.
  133.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  134.      *         is corrupt or invalid
  135.      */
  136.     public function domain($ipAddress)
  137.     {
  138.         return $this->flatModelFor(
  139.             'Domain',
  140.             'GeoIP2-Domain',
  141.             $ipAddress
  142.         );
  143.     }
  144.     /**
  145.      * This method returns a GeoIP2 Enterprise model.
  146.      *
  147.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  148.      *
  149.      * @return \GeoIp2\Model\Enterprise
  150.      *
  151.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  152.      *         not in the database.
  153.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  154.      *         is corrupt or invalid
  155.      */
  156.     public function enterprise($ipAddress)
  157.     {
  158.         return $this->modelFor('Enterprise''Enterprise'$ipAddress);
  159.     }
  160.     /**
  161.      * This method returns a GeoIP2 ISP model.
  162.      *
  163.      * @param string $ipAddress IPv4 or IPv6 address as a string.
  164.      *
  165.      * @return \GeoIp2\Model\Isp
  166.      *
  167.      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
  168.      *         not in the database.
  169.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
  170.      *         is corrupt or invalid
  171.      */
  172.     public function isp($ipAddress)
  173.     {
  174.         return $this->flatModelFor(
  175.             'Isp',
  176.             'GeoIP2-ISP',
  177.             $ipAddress
  178.         );
  179.     }
  180.     private function modelFor($class$type$ipAddress)
  181.     {
  182.         $record $this->getRecord($class$type$ipAddress);
  183.         $record['traits']['ip_address'] = $ipAddress;
  184.         $class "GeoIp2\\Model\\" $class;
  185.         return new $class($record$this->locales);
  186.     }
  187.     private function flatModelFor($class$type$ipAddress)
  188.     {
  189.         $record $this->getRecord($class$type$ipAddress);
  190.         $record['ip_address'] = $ipAddress;
  191.         $class "GeoIp2\\Model\\" $class;
  192.         return new $class($record);
  193.     }
  194.     private function getRecord($class$type$ipAddress)
  195.     {
  196.         if (strpos($this->metadata()->databaseType$type) === false) {
  197.             $method lcfirst($class);
  198.             throw new \BadMethodCallException(
  199.                 "The $method method cannot be used to open a "
  200.                 $this->metadata()->databaseType " database"
  201.             );
  202.         }
  203.         $record $this->dbReader->get($ipAddress);
  204.         if ($record === null) {
  205.             throw new AddressNotFoundException(
  206.                 "The address $ipAddress is not in the database."
  207.             );
  208.         }
  209.         if (!is_array($record)) {
  210.             // This can happen on corrupt databases. Generally,
  211.             // MaxMind\Db\Reader will throw a
  212.             // MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
  213.             // the lookup may result in a record that looks valid but is not
  214.             // an array. This mostly happens when the user is ignoring all
  215.             // exceptions and the more frequent InvalidDatabaseException
  216.             // exceptions go unnoticed.
  217.             throw new InvalidDatabaseException(
  218.                 "Expected an array when looking up $ipAddress but received: "
  219.                 gettype($record)
  220.             );
  221.         }
  222.         return $record;
  223.     }
  224.     /**
  225.      * @throws \InvalidArgumentException if arguments are passed to the method.
  226.      * @throws \BadMethodCallException if the database has been closed.
  227.      * @return \MaxMind\Db\Reader\Metadata object for the database.
  228.      */
  229.     public function metadata()
  230.     {
  231.         return $this->dbReader->metadata();
  232.     }
  233.     /**
  234.      * Closes the GeoIP2 database and returns the resources to the system.
  235.      */
  236.     public function close()
  237.     {
  238.         $this->dbReader->close();
  239.     }
  240. }