Skip to content

Commit

Permalink
Only include set fields in jsonSerlialize output
Browse files Browse the repository at this point in the history
This is more similar to the previous version and the output is much
more usable, particularly for things like Country.
  • Loading branch information
oschwald committed Oct 30, 2023
1 parent c39fad7 commit 827e756
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 139 deletions.
33 changes: 23 additions & 10 deletions src/Model/AnonymousIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,28 @@ public function __construct(array $raw)

public function jsonSerialize(): ?array
{
return [
'is_anonymous' => $this->isAnonymous,
'is_anonymous_vpn' => $this->isAnonymousVpn,
'is_hosting_provider' => $this->isHostingProvider,
'is_public_proxy' => $this->isPublicProxy,
'is_residential_proxy' => $this->isResidentialProxy,
'is_tor_exit_node' => $this->isTorExitNode,
'ip_address' => $this->ipAddress,
'network' => $this->network,
];
$js = [];
if ($this->isAnonymous !== null) {
$js['is_anonymous'] = $this->isAnonymous;
}
if ($this->isAnonymousVpn !== null) {
$js['is_anonymous_vpn'] = $this->isAnonymousVpn;
}
if ($this->isHostingProvider !== null) {
$js['is_hosting_provider'] = $this->isHostingProvider;
}
if ($this->isPublicProxy !== null) {
$js['is_public_proxy'] = $this->isPublicProxy;
}
if ($this->isResidentialProxy !== null) {
$js['is_residential_proxy'] = $this->isResidentialProxy;
}
if ($this->isTorExitNode !== null) {
$js['is_tor_exit_node'] = $this->isTorExitNode;
}
$js['ip_address'] = $this->ipAddress;
$js['network'] = $this->network;

return $js;
}
}
18 changes: 12 additions & 6 deletions src/Model/Asn.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ public function __construct(array $raw)

public function jsonSerialize(): ?array
{
return [
'autonomous_system_number' => $this->autonomousSystemNumber,
'autonomous_system_organization' => $this->autonomousSystemOrganization,
'ip_address' => $this->ipAddress,
'network' => $this->network,
];
$js = [];

if ($this->autonomousSystemNumber !== null) {
$js['autonomous_system_number'] = $this->autonomousSystemNumber;
}
if ($this->autonomousSystemOrganization !== null) {
$js['autonomous_system_organization'] = $this->autonomousSystemOrganization;
}
$js['ip_address'] = $this->ipAddress;
$js['network'] = $this->network;

return $js;
}
}
24 changes: 20 additions & 4 deletions src/Model/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace GeoIp2\Model;

use GeoIp2\Record\Subdivision;

/**
* Model class for the data returned by City Plus web service and City
* database.
Expand Down Expand Up @@ -94,15 +96,29 @@ public function jsonSerialize(): ?array
{
$js = parent::jsonSerialize();

$js['city'] = $this->city->jsonSerialize();
$js['location'] = $this->location->jsonSerialize();
$js['postal'] = $this->postal->jsonSerialize();
$city = $this->city->jsonSerialize();
if (!empty($city)) {
$js['city'] = $city;
}

$location = $this->location->jsonSerialize();
if (!empty($location)) {
$js['location'] = $location;
}

$postal =
$this->postal->jsonSerialize();
if (!empty($postal)) {
$js['postal'] = $postal;
}

$subdivisions = [];
foreach ($this->subdivisions as $sub) {
$subdivisions[] = $sub->jsonSerialize();
}
$js['subdivisions'] = $subdivisions;
if (!empty($subdivisions)) {
$js['subdivisions'] = $subdivisions;
}

return $js;
}
Expand Down
13 changes: 8 additions & 5 deletions src/Model/ConnectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ public function __construct(array $raw)

public function jsonSerialize(): ?array
{
return [
'connection_type' => $this->connectionType,
'ip_address' => $this->ipAddress,
'network' => $this->network,
];
$js = [];
if ($this->connectionType !== null) {
$js['connection_type'] = $this->connectionType;
}
$js['ip_address'] = $this->ipAddress;
$js['network'] = $this->network;

return $js;
}
}
35 changes: 27 additions & 8 deletions src/Model/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,32 @@ public function __construct(array $raw, array $locales = ['en'])

public function jsonSerialize(): ?array
{
return [
'continent' => $this->continent->jsonSerialize(),
'country' => $this->country->jsonSerialize(),
'maxmind' => $this->maxmind->jsonSerialize(),
'registered_country' => $this->registeredCountry->jsonSerialize(),
'represented_country' => $this->representedCountry->jsonSerialize(),
'traits' => $this->traits->jsonSerialize(),
];
$js = [];
$continent = $this->continent->jsonSerialize();
if (!empty($continent)) {
$js['continent'] = $continent;
}
$country = $this->country->jsonSerialize();
if (!empty($country)) {
$js['country'] = $country;
}
$maxmind = $this->maxmind->jsonSerialize();
if (!empty($maxmind)) {
$js['maxmind'] = $maxmind;
}
$registeredCountry = $this->registeredCountry->jsonSerialize();
if (!empty($registeredCountry)) {
$js['registered_country'] = $registeredCountry;
}
$representedCountry = $this->representedCountry->jsonSerialize();
if (!empty($representedCountry)) {
$js['represented_country'] = $representedCountry;
}
$traits = $this->traits->jsonSerialize();
if (!empty($traits)) {
$js['traits'] = $traits;
}

return $js;
}
}
13 changes: 8 additions & 5 deletions src/Model/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ public function __construct(array $raw)

public function jsonSerialize(): ?array
{
return [
'domain' => $this->domain,
'ip_address' => $this->ipAddress,
'network' => $this->network,
];
$js = [];
if ($this->domain !== null) {
$js['domain'] = $this->domain;
}
$js['ip_address'] = $this->ipAddress;
$js['network'] = $this->network;

return $js;
}
}
33 changes: 23 additions & 10 deletions src/Model/Isp.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,28 @@ public function __construct(array $raw)

public function jsonSerialize(): ?array
{
return [
'autonomous_system_number' => $this->autonomousSystemNumber,
'autonomous_system_organization' => $this->autonomousSystemOrganization,
'isp' => $this->isp,
'mobile_country_code' => $this->mobileCountryCode,
'mobile_network_code' => $this->mobileNetworkCode,
'organization' => $this->organization,
'ip_address' => $this->ipAddress,
'network' => $this->network,
];
$js = [];
if ($this->autonomousSystemNumber !== null) {
$js['autonomous_system_number'] = $this->autonomousSystemNumber;
}
if ($this->autonomousSystemOrganization !== null) {
$js['autonomous_system_organization'] = $this->autonomousSystemOrganization;
}
if ($this->isp !== null) {
$js['isp'] = $this->isp;
}
if ($this->mobileCountryCode !== null) {
$js['mobile_country_code'] = $this->mobileCountryCode;
}
if ($this->mobileNetworkCode !== null) {
$js['mobile_network_code'] = $this->mobileNetworkCode;
}
if ($this->organization !== null) {
$js['organization'] = $this->organization;
}
$js['ip_address'] = $this->ipAddress;
$js['network'] = $this->network;

return $js;
}
}
9 changes: 7 additions & 2 deletions src/Record/AbstractPlaceRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ public function __construct(array $record, array $locales = ['en'])
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
$js['confidence'] = $this->confidence;
$js['geoname_id'] = $this->geonameId;
if ($this->confidence !== null) {
$js['confidence'] = $this->confidence;
}

if ($this->geonameId !== null) {
$js['geoname_id'] = $this->geonameId;
}

return $js;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Record/Continent.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public function __construct(array $record, array $locales = ['en'])
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
$js['code'] = $this->code;
$js['geoname_id'] = $this->geonameId;
if ($this->code !== null) {
$js['code'] = $this->code;
}
if ($this->geonameId !== null) {
$js['geoname_id'] = $this->geonameId;
}

return $js;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Record/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public function __construct(array $record, array $locales = ['en'])
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
$js['is_in_european_union'] = $this->isInEuropeanUnion;
$js['iso_code'] = $this->isoCode;
if ($this->isInEuropeanUnion !== false) {
$js['is_in_european_union'] = $this->isInEuropeanUnion;
}
if ($this->isoCode !== null) {
$js['iso_code'] = $this->isoCode;
}

return $js;
}
Expand Down
33 changes: 24 additions & 9 deletions src/Record/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,29 @@ public function __construct(array $record)

public function jsonSerialize(): array
{
return [
'average_income' => $this->averageIncome,
'accuracy_radius' => $this->accuracyRadius,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'metro_code' => $this->metroCode,
'population_density' => $this->populationDensity,
'time_zone' => $this->timeZone,
];
$js = [];
if ($this->averageIncome !== null) {
$js['average_income'] = $this->averageIncome;
}
if ($this->accuracyRadius !== null) {
$js['accuracy_radius'] = $this->accuracyRadius;
}
if ($this->latitude !== null) {
$js['latitude'] = $this->latitude;
}
if ($this->longitude !== null) {
$js['longitude'] = $this->longitude;
}
if ($this->metroCode !== null) {
$js['metro_code'] = $this->metroCode;
}
if ($this->populationDensity !== null) {
$js['population_density'] = $this->populationDensity;
}
if ($this->timeZone !== null) {
$js['time_zone'] = $this->timeZone;
}

return $js;
}
}
9 changes: 6 additions & 3 deletions src/Record/MaxMind.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public function __construct(array $record)

public function jsonSerialize(): array
{
return [
'queries_remaining' => $this->queriesRemaining,
];
$js = [];
if ($this->queriesRemaining !== null) {
$js['queries_remaining'] = $this->queriesRemaining;
}

return $js;
}
}
13 changes: 9 additions & 4 deletions src/Record/Postal.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ public function __construct(array $record)

public function jsonSerialize(): array
{
return [
'code' => $this->code,
'confidence' => $this->confidence,
];
$js = [];
if ($this->code !== null) {
$js['code'] = $this->code;
}
if ($this->confidence !== null) {
$js['confidence'] = $this->confidence;
}

return $js;
}
}
4 changes: 3 additions & 1 deletion src/Record/RepresentedCountry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public function __construct(array $record, array $locales = ['en'])
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
$js['type'] = $this->type;
if ($this->type !== null) {
$js['type'] = $this->type;
}

return $js;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Record/Subdivision.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function __construct(array $record, array $locales = ['en'])
public function jsonSerialize(): array
{
$js = parent::jsonSerialize();
$js['iso_code'] = $this->isoCode;
if ($this->isoCode !== null) {
$js['iso_code'] = $this->isoCode;
}

return $js;
}
Expand Down
Loading

0 comments on commit 827e756

Please sign in to comment.