1: <?php
2:
3: namespace GeoIp2\Database;
4:
5: use GeoIp2\Exception\AddressNotFoundException;
6: use GeoIp2\ProviderInterface;
7: use MaxMind\Db\Reader as DbReader;
8:
9: /**
10: * Instances of this class provide a reader for the GeoIP2 database format.
11: * IP addresses can be looked up using the <code>country</code>
12: * and <code>city</code> methods. We also provide <code>cityIspOrg</code>
13: * and <code>omni</code> methods to ease compatibility with the web service
14: * client, although we may offer the ability to specify additional databases
15: * to replicate these web services in the future (e.g., the ISP/Org database).
16: *
17: * **Usage**
18: *
19: * The basic API for this class is the same for every database. First, you
20: * create a reader object, specifying a file name. You then call the method
21: * corresponding to the specific database, passing it the IP address you want
22: * to look up.
23: *
24: * If the request succeeds, the method call will return a model class for
25: * the method you called. This model in turn contains multiple record classes,
26: * each of which represents part of the data returned by the database. If
27: * the database does not contain the requested information, the attributes
28: * on the record class will have a <code>null</code> value.
29: *
30: * If the address is not in the database, an
31: * {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
32: * thrown. If an invalid IP address is passed to one of the methods, a
33: * SPL {@link \InvalidArgumentException} will be thrown. If the database is
34: * corrupt or invalid, a {@link \MaxMind\Db\Reader\InvalidDatabaseException}
35: * will be thrown.
36: *
37: */
38: class Reader implements ProviderInterface
39: {
40: private $dbReader;
41: private $locales;
42:
43: /**
44: * Constructor.
45: *
46: * @param string $filename The path to the GeoIP2 database file.
47: * @param array $locales List of locale codes to use in name property
48: * from most preferred to least preferred.
49: * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
50: * is corrupt or invalid
51: */
52: public function __construct(
53: $filename,
54: $locales = array('en')
55: ) {
56: $this->dbReader = new DbReader($filename);
57: $this->locales = $locales;
58: }
59:
60: /**
61: * This method returns a GeoIP2 City model.
62: *
63: * @param string $ipAddress IPv4 or IPv6 address as a string.
64: *
65: * @return \GeoIp2\Model\City
66: *
67: * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
68: * not in the database.
69: * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
70: * is corrupt or invalid
71: */
72: public function city($ipAddress)
73: {
74: return $this->modelFor('City', $ipAddress);
75: }
76:
77: /**
78: * This method returns a GeoIP2 Country model.
79: *
80: * @param string $ipAddress IPv4 or IPv6 address as a string.
81: *
82: * @return \GeoIp2\Model\Country
83: *
84: * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
85: * not in the database.
86: * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
87: * is corrupt or invalid
88: */
89: public function country($ipAddress)
90: {
91: return $this->modelFor('Country', $ipAddress);
92: }
93:
94: /**
95: * This method returns a GeoIP2 City model.
96: *
97: * @param string $ipAddress IPv4 or IPv6 address as a string.
98: *
99: * @return \GeoIp2\Model\City
100: *
101: * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
102: * not in the database.
103: * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
104: * is corrupt or invalid
105: *
106: * @deprecated deprecated since version 0.7.0
107: */
108: public function cityIspOrg($ipAddress)
109: {
110: return $this->modelFor('City', $ipAddress);
111: }
112:
113: /**
114: * This method returns a GeoIP2 Insights model.
115: *
116: * @param string $ipAddress IPv4 or IPv6 address as a string.
117: *
118: * @return \GeoIp2\Model\Insights
119: *
120: * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
121: * not in the database.
122: * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
123: * is corrupt or invalid
124: *
125: * @deprecated deprecated since version 0.7.0
126: */
127: public function omni($ipAddress)
128: {
129: return $this->modelFor('Insights', $ipAddress);
130: }
131:
132: public function connectionType($ipAddress)
133: {
134: return $this->flatModelFor('ConnectionType', $ipAddress);
135: }
136:
137: public function domain($ipAddress)
138: {
139: return $this->flatModelFor('Domain', $ipAddress);
140: }
141:
142: public function isp($ipAddress)
143: {
144: return $this->flatModelFor('Isp', $ipAddress);
145: }
146:
147: private function modelFor($class, $ipAddress)
148: {
149: $record = $this->getRecord($ipAddress);
150:
151: $record['traits']['ip_address'] = $ipAddress;
152: $class = "GeoIp2\\Model\\" . $class;
153:
154: return new $class($record, $this->locales);
155: }
156:
157: private function flatModelFor($class, $ipAddress)
158: {
159: $record = $this->getRecord($ipAddress);
160:
161: $record['ip_address'] = $ipAddress;
162: $class = "GeoIp2\\Model\\" . $class;
163:
164: return new $class($record);
165: }
166:
167: private function getRecord($ipAddress)
168: {
169: $record = $this->dbReader->get($ipAddress);
170: if ($record === null) {
171: throw new AddressNotFoundException(
172: "The address $ipAddress is not in the database."
173: );
174: }
175: return $record;
176: }
177:
178: /**
179: * Closes the GeoIP2 database and returns the resources to the system.
180: */
181: public function close()
182: {
183: $this->dbReader->close();
184: }
185: }
186: