Check attribute validity and return null on empty but valid attributes

This commit is contained in:
Gregory Oschwald 2013-05-08 07:18:50 -07:00
parent 45228dfad4
commit 275bda0fc4
10 changed files with 53 additions and 22 deletions

View File

@ -4,5 +4,16 @@ namespace GeoIP2\Record;
abstract class AbstractPlaceRecord extends AbstractRecord abstract class AbstractPlaceRecord extends AbstractRecord
{ {
private $languages;
public function __construct($record, $languages){
$this->languages = $languages;
parent::__construct($record);
}
public function name() {
foreach($this->languages as $language) {
if (isset($this->names[$language])) return $this->names[$language];
}
}
} }

View File

@ -11,8 +11,13 @@ abstract class AbstractRecord
} }
public function __get($attr) { public function __get($attr) {
if (isset($this->record[$attr])) return $this->record[$attr]; $valid = in_array($attr, $this->validAttributes);
if ($valid && isset($this->record[$attr])){
throw new RuntimeException("Unknown attribute: $attr"); return $this->record[$attr];
} elseif ($valid) {
return null;
} else {
throw new \RuntimeException("Unknown attribute: $attr");
}
} }
} }

View File

@ -4,5 +4,5 @@ namespace GeoIP2\Record;
class City extends AbstractPlaceRecord class City extends AbstractPlaceRecord
{ {
protected $validAttribute = Array('confidence', 'geoname_id', 'names');
} }

View File

@ -4,5 +4,7 @@ namespace GeoIP2\Record;
class Continent extends AbstractPlaceRecord class Continent extends AbstractPlaceRecord
{ {
protected $validAttributes = Array('continent_code',
'geoname_id',
'names');
} }

View File

@ -5,17 +5,8 @@ namespace GeoIP2\Record;
class Country extends AbstractPlaceRecord class Country extends AbstractPlaceRecord
{ {
private $languages; protected $validAttributes = Array('confidence',
'geoname_id',
public function __construct($record, $languages){ 'iso_code',
$this->languages = $languages; 'names');
parent::__construct($record);
}
public function name() {
foreach($this->languages as $language) {
if (isset($this->names[$language])) return $this->names[$language];
}
}
} }

View File

@ -4,5 +4,11 @@ namespace GeoIP2\Record;
class Location extends AbstractRecord class Location extends AbstractRecord
{ {
protected $validAttributes = Array('accuracy_radius',
'latitude',
'longitude',
'metro_code',
'postal_code',
'postal_confidence',
'time_zone');
} }

View File

@ -4,5 +4,5 @@ namespace GeoIP2\Record;
class Postal extends AbstractRecord class Postal extends AbstractRecord
{ {
protected $validAttributes = Array('code', 'confidence');
} }

View File

@ -4,5 +4,9 @@ namespace GeoIP2\Record;
class RepresentedCountry extends Country class RepresentedCountry extends Country
{ {
protected $validAttributes = Array('confidence',
'geoname_id',
'iso_code',
'names',
'type');
} }

View File

@ -4,5 +4,8 @@ namespace GeoIP2\Record;
class Subdivision extends AbstractPlaceRecord class Subdivision extends AbstractPlaceRecord
{ {
protected $validAttributes = Array('confidence',
'geoname_id',
'iso_code',
'names');
} }

View File

@ -4,5 +4,14 @@ namespace GeoIP2\Record;
class Traits extends AbstractRecord class Traits extends AbstractRecord
{ {
protected $validAttributes = Array('autonomous_system_number',
'autonomous_system_organization',
'domain',
'is_anonymous_proxy',
'is_satellite_provider',
'isp',
'ip_address',
'organization',
'user_type');
} }