diff --git a/src/GeoIP2/Model/City.php b/src/GeoIP2/Model/City.php index 3a480d6..ecd5c46 100644 --- a/src/GeoIP2/Model/City.php +++ b/src/GeoIP2/Model/City.php @@ -5,15 +5,31 @@ namespace GeoIP2\Model; class City extends Country { //XXX use properties - public $city; - public $location; - public $postal; - public $subdivisions; + private $city; + private $location; + private $postal; + private $subdivisions = Array(); - public function __construct($raw, $language) + public function __construct($raw, $languages) { + $this->city = new \GeoIP2\Record\City($this->get('city'), $languages); + $this->location = new \GeoIP2\Record\Location($this->get('location')); + $this->postal = new \GeoIP2\Record\Postal($this->get('postal')); - parent::__construct($raw, $language); + $this->createSubdivisions($raw, $languages); + + parent::__construct($raw, $languages); } + private function createSubdivisions($raw, $languages) { + if(!isset($raw['subdivisions'])) return; + + foreach ($raw['subdivisions'] as $sub) { + array_push($this->subdivisions, + new \GeoIP2\Record\Subdivision($sub, $languages)); + } + } + + + } \ No newline at end of file diff --git a/src/GeoIP2/Model/Country.php b/src/GeoIP2/Model/Country.php index 99e669a..4854af0 100644 --- a/src/GeoIP2/Model/Country.php +++ b/src/GeoIP2/Model/Country.php @@ -4,25 +4,36 @@ namespace GeoIP2\Model; class Country { - // XXX - use __get__ - public $continent; - public $country; - public $registeredCountry; - public $representedCountry; - public $traits; - public $raw; + private $continent; + private $country; + private $registeredCountry; + private $representedCountry; + private $traits; + private $raw; 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->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) { + protected function get($field) { return isset($this->raw[$field]) ? $this->raw[$field] : Array(); } + + public function __get ($var) + { + if ($var != "instance" && isset($this->$var)) return $this->$var; + + throw new RuntimeException("Unknown attribute: $attr"); + } } diff --git a/src/GeoIP2/Record/Postal.php b/src/GeoIP2/Record/Postal.php new file mode 100644 index 0000000..80e0693 --- /dev/null +++ b/src/GeoIP2/Record/Postal.php @@ -0,0 +1,8 @@ +