Skip to content

Commit

Permalink
+ Magic getter
Browse files Browse the repository at this point in the history
  • Loading branch information
dusterio committed Jun 10, 2016
1 parent fab805a commit 0d64ea0
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 155 deletions.
20 changes: 5 additions & 15 deletions src/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}
7 changes: 0 additions & 7 deletions src/Contracts/IdentifiableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
*/
interface IdentifiableInterface
{
/**
* Unique code
*
* @return string|int
*/
public function getCode();

/**
* @return bool
*/
Expand Down
104 changes: 12 additions & 92 deletions src/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand Down
24 changes: 7 additions & 17 deletions src/Divisible.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,7 +16,7 @@
*/
abstract class Divisible implements IdentifiableInterface
{
use HasConfig;
use HasConfig, ExposesFields;

/**
* @var array $meta
Expand Down Expand Up @@ -52,6 +53,11 @@ abstract class Divisible implements IdentifiableInterface
*/
protected $parentCode;

/**
* @var array
*/
protected $exposed = [];

/**
* Country constructor.
* @param array $meta
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Earth.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public function getCode()
return 'SOL-III';
}

/**
* @return array
*/
public function toArray()
{
return [
'code' => $this->getCode(),
'name' => $this->getName()
];
}

/**
* @return null
*/
Expand Down
29 changes: 6 additions & 23 deletions src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
73 changes: 73 additions & 0 deletions src/Traits/ExposesFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace MenaraSolutions\Geographer\Traits;

use MenaraSolutions\Geographer\Exceptions\UnknownFieldException;

/**
* Class ExposedFields
*/
trait ExposesFields
{
/**
* @return array
*/
public function toArray()
{
$array = [
'name' => $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');
}
}
2 changes: 1 addition & 1 deletion tests/Geographer/Integration/CountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit 0d64ea0

Please sign in to comment.