From 275bda0fc48cf7d586a5345b96615ae1376ec479 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 8 May 2013 07:18:50 -0700 Subject: [PATCH] Check attribute validity and return null on empty but valid attributes --- src/GeoIP2/Record/AbstractPlaceRecord.php | 11 +++++++++++ src/GeoIP2/Record/AbstractRecord.php | 11 ++++++++--- src/GeoIP2/Record/City.php | 2 +- src/GeoIP2/Record/Continent.php | 4 +++- src/GeoIP2/Record/Country.php | 17 ++++------------- src/GeoIP2/Record/Location.php | 8 +++++++- src/GeoIP2/Record/Postal.php | 2 +- src/GeoIP2/Record/RepresentedCountry.php | 6 +++++- src/GeoIP2/Record/Subdivision.php | 5 ++++- src/GeoIP2/Record/Traits.php | 9 +++++++++ 10 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/GeoIP2/Record/AbstractPlaceRecord.php b/src/GeoIP2/Record/AbstractPlaceRecord.php index 2726c62..54800d1 100644 --- a/src/GeoIP2/Record/AbstractPlaceRecord.php +++ b/src/GeoIP2/Record/AbstractPlaceRecord.php @@ -4,5 +4,16 @@ namespace GeoIP2\Record; 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]; + } + } } \ No newline at end of file diff --git a/src/GeoIP2/Record/AbstractRecord.php b/src/GeoIP2/Record/AbstractRecord.php index 09da27d..e1aae72 100644 --- a/src/GeoIP2/Record/AbstractRecord.php +++ b/src/GeoIP2/Record/AbstractRecord.php @@ -11,8 +11,13 @@ abstract class AbstractRecord } public function __get($attr) { - if (isset($this->record[$attr])) return $this->record[$attr]; - - throw new RuntimeException("Unknown attribute: $attr"); + $valid = in_array($attr, $this->validAttributes); + if ($valid && isset($this->record[$attr])){ + return $this->record[$attr]; + } elseif ($valid) { + return null; + } else { + throw new \RuntimeException("Unknown attribute: $attr"); + } } } \ No newline at end of file diff --git a/src/GeoIP2/Record/City.php b/src/GeoIP2/Record/City.php index 9a1454e..fe000ec 100644 --- a/src/GeoIP2/Record/City.php +++ b/src/GeoIP2/Record/City.php @@ -4,5 +4,5 @@ namespace GeoIP2\Record; class City extends AbstractPlaceRecord { - + protected $validAttribute = Array('confidence', 'geoname_id', 'names'); } \ No newline at end of file diff --git a/src/GeoIP2/Record/Continent.php b/src/GeoIP2/Record/Continent.php index efb0373..7a1967b 100644 --- a/src/GeoIP2/Record/Continent.php +++ b/src/GeoIP2/Record/Continent.php @@ -4,5 +4,7 @@ namespace GeoIP2\Record; class Continent extends AbstractPlaceRecord { - + protected $validAttributes = Array('continent_code', + 'geoname_id', + 'names'); } \ No newline at end of file diff --git a/src/GeoIP2/Record/Country.php b/src/GeoIP2/Record/Country.php index 07f8d59..7af965a 100644 --- a/src/GeoIP2/Record/Country.php +++ b/src/GeoIP2/Record/Country.php @@ -5,17 +5,8 @@ namespace GeoIP2\Record; class Country extends AbstractPlaceRecord { - 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]; - } - } - + protected $validAttributes = Array('confidence', + 'geoname_id', + 'iso_code', + 'names'); } \ No newline at end of file diff --git a/src/GeoIP2/Record/Location.php b/src/GeoIP2/Record/Location.php index dde5aeb..208ce0e 100644 --- a/src/GeoIP2/Record/Location.php +++ b/src/GeoIP2/Record/Location.php @@ -4,5 +4,11 @@ namespace GeoIP2\Record; class Location extends AbstractRecord { - + protected $validAttributes = Array('accuracy_radius', + 'latitude', + 'longitude', + 'metro_code', + 'postal_code', + 'postal_confidence', + 'time_zone'); } \ No newline at end of file diff --git a/src/GeoIP2/Record/Postal.php b/src/GeoIP2/Record/Postal.php index 80e0693..bfd9344 100644 --- a/src/GeoIP2/Record/Postal.php +++ b/src/GeoIP2/Record/Postal.php @@ -4,5 +4,5 @@ namespace GeoIP2\Record; class Postal extends AbstractRecord { - + protected $validAttributes = Array('code', 'confidence'); } diff --git a/src/GeoIP2/Record/RepresentedCountry.php b/src/GeoIP2/Record/RepresentedCountry.php index b8c83f1..9368d84 100644 --- a/src/GeoIP2/Record/RepresentedCountry.php +++ b/src/GeoIP2/Record/RepresentedCountry.php @@ -4,5 +4,9 @@ namespace GeoIP2\Record; class RepresentedCountry extends Country { - + protected $validAttributes = Array('confidence', + 'geoname_id', + 'iso_code', + 'names', + 'type'); } \ No newline at end of file diff --git a/src/GeoIP2/Record/Subdivision.php b/src/GeoIP2/Record/Subdivision.php index 0da51f1..dbe5301 100644 --- a/src/GeoIP2/Record/Subdivision.php +++ b/src/GeoIP2/Record/Subdivision.php @@ -4,5 +4,8 @@ namespace GeoIP2\Record; class Subdivision extends AbstractPlaceRecord { - + protected $validAttributes = Array('confidence', + 'geoname_id', + 'iso_code', + 'names'); } \ No newline at end of file diff --git a/src/GeoIP2/Record/Traits.php b/src/GeoIP2/Record/Traits.php index a7bcdc0..6aac032 100644 --- a/src/GeoIP2/Record/Traits.php +++ b/src/GeoIP2/Record/Traits.php @@ -4,5 +4,14 @@ namespace GeoIP2\Record; 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'); } \ No newline at end of file