Skip to content

Commit 36c23f0

Browse files
committed
GeoIP2 v0.9.1 PHP Extension
- added Metadata class and dummy method
1 parent aefa9df commit 36c23f0

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"name": "geoip2",
5757
"description": "GeoIP2 PHP API",
5858
"author": "github.com/php-extensions",
59-
"version": "0.9.0",
59+
"version": "0.9.1",
6060
"verbose": false,
6161
"requires": {
6262
"extensions": ["maxminddb", "json"]

geoip2/Database/Reader.zep

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
21
namespace GeoIP2\Database;
32

43
use GeoIP2\Exception\AddressNotFoundException;
54
use GeoIP2\Exception\InvalidDatabaseException;
65
use GeoIP2\ProviderInterface;
6+
use GeoIP2\Database\Reader\Metadata;
77
use MaxMind\Db\Reader as DbReader;
88

99
class Reader implements ProviderInterface
1010
{
1111
private dbReader;
1212
private locales;
1313

14+
private metadata;
15+
1416
/**
1517
* Constructor.
1618
*
@@ -28,7 +30,6 @@ class Reader implements ProviderInterface
2830
} elseif typeof locales == "string" {
2931
let this->locales = [locales];
3032
}
31-
//let this->locales = locales;
3233
}
3334

3435
/**
@@ -219,6 +220,35 @@ class Reader implements ProviderInterface
219220
}
220221
return record;
221222
}
223+
224+
/**
225+
* @return \GeoIP2\Database\Reader\Metadata object for the database
226+
*/
227+
public function metadata()
228+
{
229+
if empty this->metadata {
230+
var md = [
231+
"binary_format_major_version": "",
232+
"binary_format_minor_version": "",
233+
"build_epoch": "",
234+
"database_type": "",
235+
"languages": "",
236+
"description": "",
237+
"ip_version": "",
238+
"node_count": 0,
239+
"record_size": 0
240+
];
241+
var record = this->dbReader->get("128.101.101.101");
242+
if isset record["city"]["names"] {
243+
let md["database_type"] = "GeoIP2-City";
244+
} elseif isset record["country"]["names"] {
245+
let md["database_type"] = "GeoIP2-Country";
246+
}
247+
let this->metadata = new Metadata(md);
248+
}
249+
return this->metadata;
250+
}
251+
222252
/**
223253
* Closes the GeoIP2 database and returns the resources to the system.
224254
*/

geoip2/Database/Reader/Metadata.zep

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace GeoIP2\Database\Reader;
2+
/**
3+
* This class provides the metadata for the MaxMind DB file.
4+
*
5+
* @property int nodeCount This is an unsigned 32-bit integer indicating
6+
* the number of nodes in the search tree.
7+
* @property int recordSize This is an unsigned 16-bit integer. It
8+
* indicates the number of bits in a record in the search tree. Note that each
9+
* node consists of two records.
10+
* @property int ipVersion This is an unsigned 16-bit integer which is
11+
* always 4 or 6. It indicates whether the database contains IPv4 or IPv6
12+
* address data.
13+
* @property string databaseType This is a string that indicates the structure
14+
* of each data record associated with an IP address. The actual definition of
15+
* these structures is left up to the database creator.
16+
* @property array languages An array of strings, each of which is a language
17+
* code. A given record may contain data items that have been localized to
18+
* some or all of these languages. This may be undefined.
19+
* @property int binaryFormatMajorVersion This is an unsigned 16-bit
20+
* integer indicating the major version number for the database's binary
21+
* format.
22+
* @property int binaryFormatMinorVersion This is an unsigned 16-bit
23+
* integer indicating the minor version number for the database's binary format.
24+
* @property int buildEpoch This is an unsigned 64-bit integer that
25+
* contains the database build timestamp as a Unix epoch value.
26+
* @property array description This key will always point to a map
27+
* (associative array). The keys of that map will be language codes, and the
28+
* values will be a description in that language as a UTF-8 string. May be
29+
* undefined for some databases.
30+
*/
31+
class Metadata
32+
{
33+
private binaryFormatMajorVersion;
34+
private binaryFormatMinorVersion;
35+
private buildEpoch;
36+
private databaseType;
37+
private description;
38+
private ipVersion;
39+
private languages;
40+
private nodeByteSize;
41+
private nodeCount;
42+
private recordSize;
43+
private searchTreeSize;
44+
45+
public function __construct(var metadata)
46+
{
47+
let this->binaryFormatMajorVersion =
48+
metadata["binary_format_major_version"];
49+
let this->binaryFormatMinorVersion =
50+
metadata["binary_format_minor_version"];
51+
let this->buildEpoch = metadata["build_epoch"];
52+
let this->databaseType = metadata["database_type"];
53+
let this->languages = metadata["languages"];
54+
let this->description = metadata["description"];
55+
let this->ipVersion = metadata["ip_version"];
56+
let this->nodeCount = metadata["node_count"];
57+
let this->recordSize = metadata["record_size"];
58+
let this->nodeByteSize = this->recordSize / 4;
59+
let this->searchTreeSize = this->nodeCount * this->nodeByteSize;
60+
}
61+
public function __get(var v)
62+
{
63+
return this->{v};
64+
}
65+
}

0 commit comments

Comments
 (0)