3434class Reader implements ProviderInterface
3535{
3636 private $ dbReader ;
37+ private $ dbType ;
3738 private $ locales ;
3839
3940 /**
@@ -51,6 +52,7 @@ public function __construct(
5152 $ locales = ['en ' ]
5253 ) {
5354 $ this ->dbReader = new DbReader ($ filename );
55+ $ this ->dbType = $ this ->dbReader ->metadata ()->databaseType ;
5456 $ this ->locales = $ locales ;
5557 }
5658
@@ -212,34 +214,36 @@ public function isp($ipAddress)
212214
213215 private function modelFor ($ class , $ type , $ ipAddress )
214216 {
215- $ record = $ this ->getRecord ($ class , $ type , $ ipAddress );
217+ list ( $ record, $ prefixLen ) = $ this ->getRecord ($ class , $ type , $ ipAddress );
216218
217219 $ record ['traits ' ]['ip_address ' ] = $ ipAddress ;
220+ $ record ['traits ' ]['prefix_len ' ] = $ prefixLen ;
221+
218222 $ class = 'GeoIp2 \\Model \\' . $ class ;
219223
220224 return new $ class ($ record , $ this ->locales );
221225 }
222226
223227 private function flatModelFor ($ class , $ type , $ ipAddress )
224228 {
225- $ record = $ this ->getRecord ($ class , $ type , $ ipAddress );
229+ list ( $ record, $ prefixLen ) = $ this ->getRecord ($ class , $ type , $ ipAddress );
226230
227231 $ record ['ip_address ' ] = $ ipAddress ;
232+ $ record ['prefix_len ' ] = $ prefixLen ;
228233 $ class = 'GeoIp2 \\Model \\' . $ class ;
229234
230235 return new $ class ($ record );
231236 }
232237
233238 private function getRecord ($ class , $ type , $ ipAddress )
234239 {
235- if (strpos ($ this ->metadata ()-> databaseType , $ type ) === false ) {
240+ if (strpos ($ this ->dbType , $ type ) === false ) {
236241 $ method = lcfirst ($ class );
237242 throw new \BadMethodCallException (
238- "The $ method method cannot be used to open a "
239- . $ this ->metadata ()->databaseType . ' database '
243+ "The $ method method cannot be used to open a {$ this ->dbType } database "
240244 );
241245 }
242- $ record = $ this ->dbReader ->get ($ ipAddress );
246+ list ( $ record, $ prefixLen ) = $ this ->dbReader ->getWithPrefixLen ($ ipAddress );
243247 if ($ record === null ) {
244248 throw new AddressNotFoundException (
245249 "The address $ ipAddress is not in the database. "
@@ -259,7 +263,7 @@ private function getRecord($class, $type, $ipAddress)
259263 );
260264 }
261265
262- return $ record ;
266+ return [ $ record, $ prefixLen ] ;
263267 }
264268
265269 /**
0 commit comments