Skip to content

Commit 4337056

Browse files
committed
Only include set fields in jsonSerlialize output
This is more similar to the previous version and the output is much more usable, particularly for things like Country.
1 parent 37774da commit 4337056

18 files changed

+257
-139
lines changed

src/Model/AnonymousIp.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,28 @@ public function __construct(array $raw)
5858

5959
public function jsonSerialize(): ?array
6060
{
61-
return [
62-
'is_anonymous' => $this->isAnonymous,
63-
'is_anonymous_vpn' => $this->isAnonymousVpn,
64-
'is_hosting_provider' => $this->isHostingProvider,
65-
'is_public_proxy' => $this->isPublicProxy,
66-
'is_residential_proxy' => $this->isResidentialProxy,
67-
'is_tor_exit_node' => $this->isTorExitNode,
68-
'ip_address' => $this->ipAddress,
69-
'network' => $this->network,
70-
];
61+
$js = [];
62+
if ($this->isAnonymous !== null) {
63+
$js['is_anonymous'] = $this->isAnonymous;
64+
}
65+
if ($this->isAnonymousVpn !== null) {
66+
$js['is_anonymous_vpn'] = $this->isAnonymousVpn;
67+
}
68+
if ($this->isHostingProvider !== null) {
69+
$js['is_hosting_provider'] = $this->isHostingProvider;
70+
}
71+
if ($this->isPublicProxy !== null) {
72+
$js['is_public_proxy'] = $this->isPublicProxy;
73+
}
74+
if ($this->isResidentialProxy !== null) {
75+
$js['is_residential_proxy'] = $this->isResidentialProxy;
76+
}
77+
if ($this->isTorExitNode !== null) {
78+
$js['is_tor_exit_node'] = $this->isTorExitNode;
79+
}
80+
$js['ip_address'] = $this->ipAddress;
81+
$js['network'] = $this->network;
82+
83+
return $js;
7184
}
7285
}

src/Model/Asn.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ public function __construct(array $raw)
4242

4343
public function jsonSerialize(): ?array
4444
{
45-
return [
46-
'autonomous_system_number' => $this->autonomousSystemNumber,
47-
'autonomous_system_organization' => $this->autonomousSystemOrganization,
48-
'ip_address' => $this->ipAddress,
49-
'network' => $this->network,
50-
];
45+
$js = [];
46+
47+
if ($this->autonomousSystemNumber !== null) {
48+
$js['autonomous_system_number'] = $this->autonomousSystemNumber;
49+
}
50+
if ($this->autonomousSystemOrganization !== null) {
51+
$js['autonomous_system_organization'] = $this->autonomousSystemOrganization;
52+
}
53+
$js['ip_address'] = $this->ipAddress;
54+
$js['network'] = $this->network;
55+
56+
return $js;
5157
}
5258
}

src/Model/City.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace GeoIp2\Model;
66

7+
use GeoIp2\Record\Subdivision;
8+
79
/**
810
* Model class for the data returned by City Plus web service and City
911
* database.
@@ -94,15 +96,29 @@ public function jsonSerialize(): ?array
9496
{
9597
$js = parent::jsonSerialize();
9698

97-
$js['city'] = $this->city->jsonSerialize();
98-
$js['location'] = $this->location->jsonSerialize();
99-
$js['postal'] = $this->postal->jsonSerialize();
99+
$city = $this->city->jsonSerialize();
100+
if (!empty($city)) {
101+
$js['city'] = $city;
102+
}
103+
104+
$location = $this->location->jsonSerialize();
105+
if (!empty($location)) {
106+
$js['location'] = $location;
107+
}
108+
109+
$postal =
110+
$this->postal->jsonSerialize();
111+
if (!empty($postal)) {
112+
$js['postal'] = $postal;
113+
}
100114

101115
$subdivisions = [];
102116
foreach ($this->subdivisions as $sub) {
103117
$subdivisions[] = $sub->jsonSerialize();
104118
}
105-
$js['subdivisions'] = $subdivisions;
119+
if (!empty($subdivisions)) {
120+
$js['subdivisions'] = $subdivisions;
121+
}
106122

107123
return $js;
108124
}

src/Model/ConnectionType.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ public function __construct(array $raw)
3737

3838
public function jsonSerialize(): ?array
3939
{
40-
return [
41-
'connection_type' => $this->connectionType,
42-
'ip_address' => $this->ipAddress,
43-
'network' => $this->network,
44-
];
40+
$js = [];
41+
if ($this->connectionType !== null) {
42+
$js['connection_type'] = $this->connectionType;
43+
}
44+
$js['ip_address'] = $this->ipAddress;
45+
$js['network'] = $this->network;
46+
47+
return $js;
4548
}
4649
}

src/Model/Country.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,32 @@ public function __construct(array $raw, array $locales = ['en'])
6363

6464
public function jsonSerialize(): ?array
6565
{
66-
return [
67-
'continent' => $this->continent->jsonSerialize(),
68-
'country' => $this->country->jsonSerialize(),
69-
'maxmind' => $this->maxmind->jsonSerialize(),
70-
'registered_country' => $this->registeredCountry->jsonSerialize(),
71-
'represented_country' => $this->representedCountry->jsonSerialize(),
72-
'traits' => $this->traits->jsonSerialize(),
73-
];
66+
$js = [];
67+
$continent = $this->continent->jsonSerialize();
68+
if (!empty($continent)) {
69+
$js['continent'] = $continent;
70+
}
71+
$country = $this->country->jsonSerialize();
72+
if (!empty($country)) {
73+
$js['country'] = $country;
74+
}
75+
$maxmind = $this->maxmind->jsonSerialize();
76+
if (!empty($maxmind)) {
77+
$js['maxmind'] = $maxmind;
78+
}
79+
$registeredCountry = $this->registeredCountry->jsonSerialize();
80+
if (!empty($registeredCountry)) {
81+
$js['registered_country'] = $registeredCountry;
82+
}
83+
$representedCountry = $this->representedCountry->jsonSerialize();
84+
if (!empty($representedCountry)) {
85+
$js['represented_country'] = $representedCountry;
86+
}
87+
$traits = $this->traits->jsonSerialize();
88+
if (!empty($traits)) {
89+
$js['traits'] = $traits;
90+
}
91+
92+
return $js;
7493
}
7594
}

src/Model/Domain.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ public function __construct(array $raw)
3737

3838
public function jsonSerialize(): ?array
3939
{
40-
return [
41-
'domain' => $this->domain,
42-
'ip_address' => $this->ipAddress,
43-
'network' => $this->network,
44-
];
40+
$js = [];
41+
if ($this->domain !== null) {
42+
$js['domain'] = $this->domain;
43+
}
44+
$js['ip_address'] = $this->ipAddress;
45+
$js['network'] = $this->network;
46+
47+
return $js;
4548
}
4649
}

src/Model/Isp.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,28 @@ public function __construct(array $raw)
6161

6262
public function jsonSerialize(): ?array
6363
{
64-
return [
65-
'autonomous_system_number' => $this->autonomousSystemNumber,
66-
'autonomous_system_organization' => $this->autonomousSystemOrganization,
67-
'isp' => $this->isp,
68-
'mobile_country_code' => $this->mobileCountryCode,
69-
'mobile_network_code' => $this->mobileNetworkCode,
70-
'organization' => $this->organization,
71-
'ip_address' => $this->ipAddress,
72-
'network' => $this->network,
73-
];
64+
$js = [];
65+
if ($this->autonomousSystemNumber !== null) {
66+
$js['autonomous_system_number'] = $this->autonomousSystemNumber;
67+
}
68+
if ($this->autonomousSystemOrganization !== null) {
69+
$js['autonomous_system_organization'] = $this->autonomousSystemOrganization;
70+
}
71+
if ($this->isp !== null) {
72+
$js['isp'] = $this->isp;
73+
}
74+
if ($this->mobileCountryCode !== null) {
75+
$js['mobile_country_code'] = $this->mobileCountryCode;
76+
}
77+
if ($this->mobileNetworkCode !== null) {
78+
$js['mobile_network_code'] = $this->mobileNetworkCode;
79+
}
80+
if ($this->organization !== null) {
81+
$js['organization'] = $this->organization;
82+
}
83+
$js['ip_address'] = $this->ipAddress;
84+
$js['network'] = $this->network;
85+
86+
return $js;
7487
}
7588
}

src/Record/AbstractPlaceRecord.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ public function __construct(array $record, array $locales = ['en'])
2323
public function jsonSerialize(): array
2424
{
2525
$js = parent::jsonSerialize();
26-
$js['confidence'] = $this->confidence;
27-
$js['geoname_id'] = $this->geonameId;
26+
if ($this->confidence !== null) {
27+
$js['confidence'] = $this->confidence;
28+
}
29+
30+
if ($this->geonameId !== null) {
31+
$js['geoname_id'] = $this->geonameId;
32+
}
2833

2934
return $js;
3035
}

src/Record/Continent.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ public function __construct(array $record, array $locales = ['en'])
4040
public function jsonSerialize(): array
4141
{
4242
$js = parent::jsonSerialize();
43-
$js['code'] = $this->code;
44-
$js['geoname_id'] = $this->geonameId;
43+
if ($this->code !== null) {
44+
$js['code'] = $this->code;
45+
}
46+
if ($this->geonameId !== null) {
47+
$js['geoname_id'] = $this->geonameId;
48+
}
4549

4650
return $js;
4751
}

src/Record/Country.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ public function __construct(array $record, array $locales = ['en'])
4646
public function jsonSerialize(): array
4747
{
4848
$js = parent::jsonSerialize();
49-
$js['is_in_european_union'] = $this->isInEuropeanUnion;
50-
$js['iso_code'] = $this->isoCode;
49+
if ($this->isInEuropeanUnion !== false) {
50+
$js['is_in_european_union'] = $this->isInEuropeanUnion;
51+
}
52+
if ($this->isoCode !== null) {
53+
$js['iso_code'] = $this->isoCode;
54+
}
5155

5256
return $js;
5357
}

0 commit comments

Comments
 (0)