From c44053c2763f3c451d80520fa3d7bac5b788045e Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Tue, 28 Oct 2014 10:27:18 -0500 Subject: [PATCH] Return false instead of null for boolean attributes --- CHANGELOG.md | 2 ++ src/GeoIp2/Model/AbstractModel.php | 10 +++++++++- src/GeoIp2/Record/AbstractRecord.php | 6 +++++- tests/GeoIp2/Test/Database/ReaderTest.php | 6 +++--- tests/GeoIp2/Test/Model/InsightsTest.php | 14 +++++++++++++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac1c035..d605870 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ CHANGELOG * Added support for the GeoIP2 Anonymous IP database. The `GeoIP2\Database\Reader` class now has an `anonymousIp` method which returns a `GeoIP2\Model\AnonymousIp` object. +* Boolean attributes like those in the `GeoIP2\Record\Traits` class now return + `false` insteadof `null` when they were not true. 2.0.0 (2014-09-22) ------------------ diff --git a/src/GeoIp2/Model/AbstractModel.php b/src/GeoIp2/Model/AbstractModel.php index cd96666..b6c7533 100644 --- a/src/GeoIp2/Model/AbstractModel.php +++ b/src/GeoIp2/Model/AbstractModel.php @@ -23,7 +23,15 @@ abstract class AbstractModel implements \JsonSerializable */ protected function get($field) { - return isset($this->raw[$field]) ? $this->raw[$field] : null; + if (isset($this->raw[$field])) { + return $this->raw[$field]; + } else { + if (preg_match('/^is_/', $field)) { + return false; + } else { + return null; + } + } } /** diff --git a/src/GeoIp2/Record/AbstractRecord.php b/src/GeoIp2/Record/AbstractRecord.php index b81bfea..42f0887 100644 --- a/src/GeoIp2/Record/AbstractRecord.php +++ b/src/GeoIp2/Record/AbstractRecord.php @@ -25,7 +25,11 @@ abstract class AbstractRecord implements \JsonSerializable if ($this->__isset($attr)) { return $this->record[$key]; } elseif ($this->validAttribute($attr)) { - return null; + if (preg_match('/^is_/', $key)) { + return false; + } else { + return null; + } } else { throw new \RuntimeException("Unknown attribute: $attr"); } diff --git a/tests/GeoIp2/Test/Database/ReaderTest.php b/tests/GeoIp2/Test/Database/ReaderTest.php index e87b4a6..ef00e74 100644 --- a/tests/GeoIp2/Test/Database/ReaderTest.php +++ b/tests/GeoIp2/Test/Database/ReaderTest.php @@ -96,9 +96,9 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $record = $reader->anonymousIp($ipAddress); $this->assertSame(true, $record->isAnonymous); $this->assertSame(true, $record->isAnonymousVpn); - $this->assertSame(null, $record->isHostingProvider); - $this->assertSame(null, $record->isPublicProxy); - $this->assertSame(null, $record->isTorExitNode); + $this->assertSame(false, $record->isHostingProvider); + $this->assertSame(false, $record->isPublicProxy); + $this->assertSame(false, $record->isTorExitNode); $this->assertEquals($ipAddress, $record->ipAddress); $reader->close(); } diff --git a/tests/GeoIp2/Test/Model/InsightsTest.php b/tests/GeoIp2/Test/Model/InsightsTest.php index 9bbb49d..e4c7a34 100644 --- a/tests/GeoIp2/Test/Model/InsightsTest.php +++ b/tests/GeoIp2/Test/Model/InsightsTest.php @@ -62,7 +62,7 @@ class InsightsTest extends \PHPUnit_Framework_TestCase 'autonomous_system_organization' => 'AS Organization', 'domain' => 'example.com', 'ip_address' => '1.2.3.4', - 'is_satellite_provider' => 1, + 'is_satellite_provider' => true, 'isp' => 'Comcast', 'organization' => 'Blorg', 'user_type' => 'college', @@ -130,6 +130,18 @@ class InsightsTest extends \PHPUnit_Framework_TestCase '$model->traits' ); + $this->assertSame( + true, + $model->traits->isSatelliteProvider, + '$model->traits->isSatelliteProvider is true' + ); + + $this->assertSame( + false, + $model->traits->isAnonymousProxy, + '$model->traits->isAnonymousProxy is false' + ); + $this->assertEquals( 22, $model->maxmind->queriesRemaining,