From 0d64ea01f6f19c2139d8047a2179f8c2e8e024ba Mon Sep 17 00:00:00 2001 From: Denis Mysenko Date: Fri, 10 Jun 2016 22:52:21 +1000 Subject: [PATCH] + Magic getter --- src/City.php | 20 +--- src/Contracts/IdentifiableInterface.php | 7 -- src/Country.php | 104 +++---------------- src/Divisible.php | 24 ++--- src/Earth.php | 11 ++ src/State.php | 29 ++---- src/Traits/ExposesFields.php | 73 +++++++++++++ tests/Geographer/Integration/CountryTest.php | 2 +- 8 files changed, 115 insertions(+), 155 deletions(-) create mode 100644 src/Traits/ExposesFields.php diff --git a/src/City.php b/src/City.php index 23ad490..2624311 100644 --- a/src/City.php +++ b/src/City.php @@ -19,20 +19,10 @@ class City extends Divisible protected static $parentClass = State::class; /** - * Unique code - * - * @return int + * @var array */ - public function getCode() - { - return $this->meta['ids']['geonames']; - } - - /** - * @return int - */ - public function getGeonamesCode() - { - return $this->getCode(); - } + protected $exposed = [ + 'code' => 'ids.geonames', + 'geonamesCode' => 'ids.geonames' + ]; } diff --git a/src/Contracts/IdentifiableInterface.php b/src/Contracts/IdentifiableInterface.php index 4f47478..58d6b7d 100644 --- a/src/Contracts/IdentifiableInterface.php +++ b/src/Contracts/IdentifiableInterface.php @@ -8,13 +8,6 @@ */ interface IdentifiableInterface { - /** - * Unique code - * - * @return string|int - */ - public function getCode(); - /** * @return bool */ diff --git a/src/Country.php b/src/Country.php index 6731503..b52861a 100644 --- a/src/Country.php +++ b/src/Country.php @@ -21,24 +21,19 @@ class Country extends Divisible protected static $parentClass = Earth::class; /** - * Get alpha2 ISO code - * - * @return string + * @var array */ - public function getCode() - { - return $this->meta['ids']['iso_3611'][0]; - } - - /** - * Get alpha3 ISO code - * - * @return string - */ - public function getCode3() - { - return $this->meta['ids']['iso_3611'][1]; - } + protected $exposed = [ + 'code' => 'ids.iso_3611.0', + 'code3' => 'ids.iso_3611.1', + 'geonamesCode' => 'ids.geonames', + 'area', + 'currency', + 'phonePrefix' => 'phone', + 'population', + 'continent', + 'language' => 'languages.0' + ]; /** * @return string @@ -48,81 +43,6 @@ public function getParentCode() return 'SOL-III'; } - /** - * - */ - public function getGeonamesCode() - { - return $this->meta['ids']['geonames']; - } - - /** - * @return int - */ - public function getArea() - { - return isset($this->meta['area']) ? $this->meta['area'] : 0; - } - - /** - * @return string - */ - public function getCurrencyCode() - { - return isset($this->meta['currency']) ? $this->meta['currency'] : false; - } - - /** - * @return int|bool - */ - public function getPhonePrefix() - { - return isset($this->meta['phone']) ? $this->meta['phone'] : false; - } - - /** - * @return int - */ - public function getPopulation() - { - return isset($this->meta['population']) ? $this->meta['population'] : 0; - } - - /** - * @return string - */ - public function getContinent() - { - return $this->meta['continent']; - } - - /** - * @return string - */ - public function getLanguage() - { - return $this->meta['languages'][0]; - } - - /** - * @return array - */ - public function toArray() - { - return [ - 'code' => $this->getCode(), - 'code_3' => $this->getCode3(), - 'name' => $this->getName(), - 'geonames_id' => $this->getGeonamesCode(), - 'area' => $this->getArea(), - 'phone_prefix' => $this->getPhonePrefix(), - 'currency_code' => $this->getCurrencyCode(), - 'population' => $this->getPopulation(), - 'continent' => $this->getContinent(), - 'language' => $this->getLanguage() - ]; - } - /** * @return bool|Divisible */ diff --git a/src/Divisible.php b/src/Divisible.php index 847ec89..b7623c8 100644 --- a/src/Divisible.php +++ b/src/Divisible.php @@ -6,6 +6,7 @@ use MenaraSolutions\Geographer\Contracts\ConfigInterface; use MenaraSolutions\Geographer\Contracts\IdentifiableInterface; use MenaraSolutions\Geographer\Services\DefaultConfig; +use MenaraSolutions\Geographer\Traits\ExposesFields; use MenaraSolutions\Geographer\Traits\HasConfig; use MenaraSolutions\Geographer\Repositories\File; @@ -15,7 +16,7 @@ */ abstract class Divisible implements IdentifiableInterface { - use HasConfig; + use HasConfig, ExposesFields; /** * @var array $meta @@ -52,6 +53,11 @@ abstract class Divisible implements IdentifiableInterface */ protected $parentCode; + /** + * @var array + */ + protected $exposed = []; + /** * Country constructor. * @param array $meta @@ -200,22 +206,6 @@ public function getMeta() { return $this->meta; } - - /** - * @return array - */ - public function toArray() - { - return [ - 'code' => $this->getCode(), - 'name' => $this->getName() - ]; - } - - /** - * @return string|int - */ - abstract public function getCode(); /** * @return string|int diff --git a/src/Earth.php b/src/Earth.php index 4926d04..67f9a76 100644 --- a/src/Earth.php +++ b/src/Earth.php @@ -48,6 +48,17 @@ public function getCode() return 'SOL-III'; } + /** + * @return array + */ + public function toArray() + { + return [ + 'code' => $this->getCode(), + 'name' => $this->getName() + ]; + } + /** * @return null */ diff --git a/src/State.php b/src/State.php index f5024a8..231d326 100644 --- a/src/State.php +++ b/src/State.php @@ -19,31 +19,14 @@ class State extends Divisible protected static $parentClass = Country::class; /** - * Get Geonames code - * - * @return int + * @var array */ - public function getCode() - { - return $this->meta['ids']['geonames']; - } + protected $exposed = [ + 'code' => 'ids.geonames', + 'fipsCode' => 'ids.fips', + 'isoCode' => 'ids.iso_3166', + ]; - /** - * @return string|bool - */ - public function getFipsCode() - { - return isset($this->meta['ids']['fips']) ? $this->meta['ids']['fips'] : false; - } - - /** - * @return string|bool - */ - public function getIsoCode() - { - return isset($this->meta['ids']['iso_3166']) ? $this->meta['ids']['iso_3166'] : false; - } - /** * @return Collections\MemberCollection */ diff --git a/src/Traits/ExposesFields.php b/src/Traits/ExposesFields.php new file mode 100644 index 0000000..38aef76 --- /dev/null +++ b/src/Traits/ExposesFields.php @@ -0,0 +1,73 @@ + $this->getName() + ]; + + foreach ($this->exposed as $key => $value) { + $array[$key] = $this->extract(empty($value) ? $key : $value); + } + + return $array; + } + + /** + * @param string $path + * @return mixed + */ + protected function extract($path) + { + $parts = explode('.', $path); + + if (count($parts) == 1) { + return isset($this->meta[$path]) ? $this->meta[$path] : null; + } + + $current = &$this->meta; + + foreach ($parts as $field) { + if (! isset($current[$field])) { + return null; + } + + $current = &$current[$field]; + } + + return $current; + } + + /** + * @param $methodName + * @param $args + * @return string|int + * @throws UnknownFieldException + */ + public function __call($methodName, $args) + { + if (preg_match('~^(get)([A-Z])(.*)$~', $methodName, $matches)) { + $field = strtolower($matches[2]) . $matches[3]; + + if (! array_key_exists($field, $this->exposed)) { + throw new UnknownFieldException('Field ' . $field . ' does not exist'); + } + + return $this->extract($this->exposed[$field]); + } + + throw new UnknownFieldException('Unknown magic getter'); + } +} \ No newline at end of file diff --git a/tests/Geographer/Integration/CountryTest.php b/tests/Geographer/Integration/CountryTest.php index 6ee1c3f..86314e6 100644 --- a/tests/Geographer/Integration/CountryTest.php +++ b/tests/Geographer/Integration/CountryTest.php @@ -24,7 +24,7 @@ public function can_fetch_states_for_all_countries() $array = $country->inflict('from')->toArray(); $this->assertTrue(is_array($array)); $this->assertArrayHasKey('code', $array); - $this->assertArrayHasKey('code_3', $array); + $this->assertArrayHasKey('code3', $array); $this->assertArrayHasKey('name', $array); // echo $array['name'] . "\n"; }