Merge pull request #31 from maxmind/dave/boolean-false-not-null

Return false instead of null for non-true boolean attributes
This commit is contained in:
Gregory Oschwald 2014-10-28 10:49:29 -07:00
commit 3142e44b3e
5 changed files with 32 additions and 6 deletions

View File

@ -9,6 +9,8 @@ CHANGELOG
* Added support for the GeoIP2 Anonymous IP database. The * Added support for the GeoIP2 Anonymous IP database. The
`GeoIP2\Database\Reader` class now has an `anonymousIp` method which returns `GeoIP2\Database\Reader` class now has an `anonymousIp` method which returns
a `GeoIP2\Model\AnonymousIp` object. 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) 2.0.0 (2014-09-22)
------------------ ------------------

View File

@ -23,7 +23,15 @@ abstract class AbstractModel implements \JsonSerializable
*/ */
protected function get($field) 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;
}
}
} }
/** /**

View File

@ -25,7 +25,11 @@ abstract class AbstractRecord implements \JsonSerializable
if ($this->__isset($attr)) { if ($this->__isset($attr)) {
return $this->record[$key]; return $this->record[$key];
} elseif ($this->validAttribute($attr)) { } elseif ($this->validAttribute($attr)) {
return null; if (preg_match('/^is_/', $key)) {
return false;
} else {
return null;
}
} else { } else {
throw new \RuntimeException("Unknown attribute: $attr"); throw new \RuntimeException("Unknown attribute: $attr");
} }

View File

@ -96,9 +96,9 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
$record = $reader->anonymousIp($ipAddress); $record = $reader->anonymousIp($ipAddress);
$this->assertSame(true, $record->isAnonymous); $this->assertSame(true, $record->isAnonymous);
$this->assertSame(true, $record->isAnonymousVpn); $this->assertSame(true, $record->isAnonymousVpn);
$this->assertSame(null, $record->isHostingProvider); $this->assertSame(false, $record->isHostingProvider);
$this->assertSame(null, $record->isPublicProxy); $this->assertSame(false, $record->isPublicProxy);
$this->assertSame(null, $record->isTorExitNode); $this->assertSame(false, $record->isTorExitNode);
$this->assertEquals($ipAddress, $record->ipAddress); $this->assertEquals($ipAddress, $record->ipAddress);
$reader->close(); $reader->close();
} }

View File

@ -62,7 +62,7 @@ class InsightsTest extends \PHPUnit_Framework_TestCase
'autonomous_system_organization' => 'AS Organization', 'autonomous_system_organization' => 'AS Organization',
'domain' => 'example.com', 'domain' => 'example.com',
'ip_address' => '1.2.3.4', 'ip_address' => '1.2.3.4',
'is_satellite_provider' => 1, 'is_satellite_provider' => true,
'isp' => 'Comcast', 'isp' => 'Comcast',
'organization' => 'Blorg', 'organization' => 'Blorg',
'user_type' => 'college', 'user_type' => 'college',
@ -130,6 +130,18 @@ class InsightsTest extends \PHPUnit_Framework_TestCase
'$model->traits' '$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( $this->assertEquals(
22, 22,
$model->maxmind->queriesRemaining, $model->maxmind->queriesRemaining,