From 01bf8c2d7c70129a559d8efa828079b42f437898 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 7 May 2013 15:02:53 -0700 Subject: [PATCH] Add support for named records. Create all the objects for Country model --- src/GeoIP2/Model/Country.php | 18 +++++++++++++----- src/GeoIP2/Record/Country.php | 13 +++++++++++++ src/GeoIP2/Webservice/Client.php | 8 ++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/GeoIP2/Model/Country.php b/src/GeoIP2/Model/Country.php index 1f9f38a..99e669a 100644 --- a/src/GeoIP2/Model/Country.php +++ b/src/GeoIP2/Model/Country.php @@ -7,14 +7,22 @@ class Country // XXX - use __get__ public $continent; public $country; - public $registered_country; - public $represented_country; + public $registeredCountry; + public $representedCountry; public $traits; public $raw; - public function __construct($raw, $language) { - $this->country = new \GeoIP2\Record\Country($raw['country']); - + public function __construct($raw, $languages) { $this->raw = $raw; + + $this->continent = new \GeoIP2\Record\Continent($this->get('continent'), $languages); + $this->country = new \GeoIP2\Record\Country($this->get('country'), $languages); + $this->registeredCountry = new \GeoIP2\Record\Country($this->get('registered_country'), $languages); + $this->representedCountry = new \GeoIP2\Record\RepresentedCountry($this->get('represented_country'), $languages); + $this->traits = new \GeoIP2\Record\Traits($this->get('traits')); + } + + private function get($field) { + return isset($this->raw[$field]) ? $this->raw[$field] : Array(); } } diff --git a/src/GeoIP2/Record/Country.php b/src/GeoIP2/Record/Country.php index 0bc1f29..07f8d59 100644 --- a/src/GeoIP2/Record/Country.php +++ b/src/GeoIP2/Record/Country.php @@ -5,4 +5,17 @@ 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]; + } + } + } \ No newline at end of file diff --git a/src/GeoIP2/Webservice/Client.php b/src/GeoIP2/Webservice/Client.php index b44b745..b7a6818 100644 --- a/src/GeoIP2/Webservice/Client.php +++ b/src/GeoIP2/Webservice/Client.php @@ -16,14 +16,14 @@ class Client private $user_id; private $license_key; - private $language; + private $languages; private $base_uri = 'https://geoip.maxmind.com/geoip/v2.0'; - public function __construct($user_id, $license_key, $language='en') + public function __construct($user_id, $license_key, $languages=array('en')) { $this->user_id = $user_id; $this->license_key = $license_key; - $this->language = $language; + $this->languages = $languages; } public function city($ip_address = 'me') @@ -62,7 +62,7 @@ class Client if ($response->isSuccessful()) { $body = $this->handleSuccess($response, $uri); $class = "GeoIP2\\Model\\" . $class; - return new $class($body, $this->language); + return new $class($body, $this->languages); } }