Skip to content

Commit

Permalink
Fixed configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
juanparati committed Nov 5, 2021
1 parent 3ac291a commit ac53b47
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 99 deletions.
4 changes: 3 additions & 1 deletion config/isocodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
'currencyNumbers'=> \Juanparati\ISOCodes\Databases\Currencies\CurrencyNumbers::class,
'languages' => \Juanparati\ISOCodes\Databases\Languages\LanguagesEN::class,
'continents' => \Juanparati\ISOCodes\Databases\Continents\ContinentsEN::class,
]
],

];
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Test</directory>
<directory suffix="Test.php">./tests/test</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions src/Contracts/ISODatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
interface ISODatabase
{
public function all() : array;
}
public function all(): array;
}
6 changes: 2 additions & 4 deletions src/Contracts/ISOModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

interface ISOModel
{

/**
* Constructor.
*
Expand All @@ -21,6 +20,5 @@ public function __construct(ISOCodes $iso);
*
* @return array
*/
public function list() : Collection;

}
public function list(): Collection;
}
2 changes: 1 addition & 1 deletion src/Databases/Continents/ContinentsEN.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class ContinentsEN extends ISODatabaseBase
'OC' => 'Oceania',
'SA' => 'South America',
];
}
}
2 changes: 1 addition & 1 deletion src/Databases/Countries/CountriesEN.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,4 @@ class CountriesEN extends ISODatabaseBase
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe',
];
}
}
6 changes: 1 addition & 5 deletions src/Databases/Countries/CountryCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Juanparati\ISOCodes\Databases\ISODatabaseBase;


/**
* List of country codes with their ISO 3166-1 alpha-2, alpha3, numeric code and TLD.
*
Expand All @@ -15,7 +14,6 @@
*/
class CountryCodes extends ISODatabaseBase
{

protected array $db = [
'AF' => [
'alpha2' => 'AF',
Expand Down Expand Up @@ -2074,6 +2072,4 @@ class CountryCodes extends ISODatabaseBase
'continents' => ['AF'],
],
];


}
}
4 changes: 1 addition & 3 deletions src/Databases/Currencies/CurrenciesEN.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class CurrenciesEN extends ISODatabaseBase
{

protected array $db = [
'AED' => 'UAE Dirham',
'AFN' => 'Afghani',
Expand Down Expand Up @@ -182,5 +181,4 @@ class CurrenciesEN extends ISODatabaseBase
'ZMW' => 'Zambian Kwacha',
'ZWL' => 'Zimbabwe Dollar',
];

}
}
2 changes: 1 addition & 1 deletion src/Databases/Currencies/CurrencyNumbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ class CurrencyNumbers extends ISODatabaseBase
'ZMW' => '967',
'ZWL' => '932',
];
}
}
3 changes: 1 addition & 2 deletions src/Databases/ISODatabaseBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Juanparati\ISOCodes\Contracts\ISODatabase;


/**
* Base class used for databases.
*/
Expand All @@ -26,4 +25,4 @@ public function all(): array
{
return $this->db;
}
}
}
5 changes: 1 addition & 4 deletions src/Databases/Languages/LanguagesEN.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class LanguagesEN extends ISODatabaseBase
{

protected array $db = [
'AA' => 'Afar',
'AB' => 'Abkhazian',
Expand Down Expand Up @@ -197,6 +196,4 @@ class LanguagesEN extends ISODatabaseBase
'ZH' => 'Chinese',
'ZU' => 'Zulu',
];


}
}
4 changes: 3 additions & 1 deletion src/Exceptions/ISOModelNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace Juanparati\ISOCodes\Exceptions;

class ISOModelNotFound extends \Exception {}
class ISOModelNotFound extends \Exception
{
}
4 changes: 3 additions & 1 deletion src/Exceptions/ISONodeAttributeMissing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace Juanparati\ISOCodes\Exceptions;

class ISONodeAttributeMissing extends \Exception {}
class ISONodeAttributeMissing extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/Facades/ISOCodesFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ protected static function getFacadeAccessor()
{
return ISOCodes::class;
}
}
}
25 changes: 13 additions & 12 deletions src/ISOCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Juanparati\ISOCodes;


use Juanparati\ISOCodes\Contracts\ISODatabase;
use Juanparati\ISOCodes\Contracts\ISOModel;
use Juanparati\ISOCodes\Exceptions\ISOModelNotFound;


/**
* Provide the main ISOCodes objects.
*
Expand All @@ -19,7 +17,6 @@
*/
class ISOCodes
{

/**
* Registered databases.
*
Expand Down Expand Up @@ -56,7 +53,8 @@ class ISOCodes
*
* @param array $databases
*/
public function __construct(array $databases = []) {
public function __construct(array $databases = [])
{
$this->databases = array_merge($this->databases, $databases);
}

Expand All @@ -69,15 +67,16 @@ public function __construct(array $databases = []) {
* @return ISOModel
* @throws ISOModelNotFound
*/
public function __call(string $name, array $args) {

public function __call(string $name, array $args)
{
$model = '\\Juanparati\\ISOCodes\\Models\\' . ucfirst($name) . 'Model';

if (!isset($this->modelInstances[$model])) {
if (class_exists($model))
if (class_exists($model)) {
$this->modelInstances[$model] = new $model($this);
else
} else {
throw new ISOModelNotFound($model . ' not found');
}
}

return $this->modelInstances[$model];
Expand All @@ -90,10 +89,12 @@ public function __call(string $name, array $args) {
* @param string
* @return ISODatabase
*/
public function getDatabaseInstance(string $key) : ISODatabase {
if (!isset($this->databaseInstances[$key]))
$this->databaseInstances[$key] = new $this->databases[$key];
public function getDatabaseInstance(string $key): ISODatabase
{
if (!isset($this->databaseInstances[$key])) {
$this->databaseInstances[$key] = new $this->databases[$key]();
}

return $this->databaseInstances[$key];
}
}
}
3 changes: 1 addition & 2 deletions src/Models/ByContinentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Juanparati\ISOCodes\Models;


class ByContinentModel extends ByNodeBase
{
protected string $database = 'continents';
Expand All @@ -11,4 +10,4 @@ class ByContinentModel extends ByNodeBase
'currencies',
'languages',
];
}
}
34 changes: 18 additions & 16 deletions src/Models/ByCountryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

class ByCountryModel extends ModelBase
{

/**
* Search by Alpha2.
*
* @param string $alpha2
* @return array|null
*/
public function byAlpha2(string $alpha2) : ?array {
public function byAlpha2(string $alpha2): ?array
{
return $this->all()
->where('alpha2', strtoupper($alpha2))
->first();
Expand All @@ -27,7 +27,8 @@ public function byAlpha2(string $alpha2) : ?array {
* @param string $alpha3
* @return array|null
*/
public function byAlpha3(string $alpha3) : ?array {
public function byAlpha3(string $alpha3): ?array
{
return $this->all()
->where('alpha3', strtoupper($alpha3))
->first();
Expand All @@ -40,7 +41,8 @@ public function byAlpha3(string $alpha3) : ?array {
* @param $number
* @return array|null
*/
public function byNumberic($number) : ?array {
public function byNumberic($number): ?array
{
return $this->all()
->where('numeric', $number)
->first();
Expand All @@ -53,7 +55,8 @@ public function byNumberic($number) : ?array {
* @param string $tld
* @return array|null
*/
public function byTld(string $tld) : ?array {
public function byTld(string $tld): ?array
{
$tld = Str::of($tld)
->start('.')
->lower();
Expand All @@ -69,10 +72,11 @@ public function byTld(string $tld) : ?array {
*
* @return Collection
*/
public function all() : Collection
public function all(): Collection
{
if ($collection = $this->getCache('all'))
if ($collection = $this->getCache('all')) {
return $collection;
}

$nodeData = [
'languages' => $this->iso->byLanguage()->list(),
Expand All @@ -83,7 +87,6 @@ public function all() : Collection
$currencyNumbers = $this->iso->byCurrencyNumber()->list();

$list = $this->list()->map(function ($country) use ($nodeData, $currencyNumbers) {

foreach (array_keys($this->nodeResolution) as $nodeName) {
if (isset($country[$nodeName])) {
$data = $this->resolveNodeData(
Expand All @@ -92,15 +95,13 @@ public function all() : Collection
$country[$nodeName]
);

if ($data === null)
if ($data === null) {
unset($country[$nodeName]);
else {

} else {
if ($this->options['currencyAsNumber'] && $nodeName === 'currencies') {

$data = match ($this->nodeResolution['currencies']) {
static::NODE_AS_CODE => array_map(fn($cur) => (string) $currencyNumbers[$cur], $data),
static::NODE_AS_ALL => collect($data)->mapWithKeys(fn($name, $cur) => [(string) $currencyNumbers[$cur] => $name])->toArray(),
static::NODE_AS_CODE => array_map(fn ($cur) => (string) $currencyNumbers[$cur], $data),
static::NODE_AS_ALL => collect($data)->mapWithKeys(fn ($name, $cur) => [(string) $currencyNumbers[$cur] => $name])->toArray(),
};
}

Expand All @@ -125,8 +126,9 @@ public function all() : Collection
*/
public function list(): Collection
{
if ($collection = $this->getCache('list'))
if ($collection = $this->getCache('list')) {
return $collection;
}

$countryNames = $this->iso->getDatabaseInstance('countries')->all();

Expand All @@ -140,4 +142,4 @@ public function list(): Collection

return $list;
}
}
}
3 changes: 1 addition & 2 deletions src/Models/ByCurrencyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Juanparati\ISOCodes\Models;


class ByCurrencyModel extends ByNodeBase
{
protected string $database = 'currencies';
Expand All @@ -11,4 +10,4 @@ class ByCurrencyModel extends ByNodeBase
'languages',
'continents',
];
}
}
Loading

0 comments on commit ac53b47

Please sign in to comment.