Skip to content

Commit

Permalink
4.x release
Browse files Browse the repository at this point in the history
  • Loading branch information
juanparati committed May 25, 2023
1 parent 2e602ab commit 95e21e9
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 43 deletions.
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Publish configuration file (Required only when custom dataset or locales are req

The list of results are returned as [Collections](https://laravel.com/docs/9.x/collections).

### Country model
### Country model examples

Get the list of all country codes as an array:

Expand Down Expand Up @@ -191,11 +191,10 @@ Retrieve a list of countries that has Portuguese as one of their official langua
->whereLanguage('PT');
```

* Note that most spoken language should be always the first in the list.
* Note that most spoken language of each country should be always the first in the list.


### Language model

### Language model examples
Get the list grouped by language:

```php
Expand Down Expand Up @@ -233,7 +232,7 @@ It returns something like:
];


### Continent model
### Continent model examples

Get the list grouped by continent.

Expand All @@ -244,7 +243,7 @@ Example:
```


### Currency model
### Currency model examples

Get the list grouped by currency.

Expand All @@ -254,7 +253,7 @@ Example:
(new ISOCodes)->currencies()->toArray();
```

### CurrencyNumber model
### CurrencyNumber model examples

Get the list grouped by currency number.

Expand Down Expand Up @@ -319,9 +318,9 @@ Examples:
```php
(new ISOCodes)
->countries()
->setResolution('currencies', CountryModel::NODE_AS_ALL)
->setResolution('languages', CountryModel::NODE_AS_NAME)
->setResolution('continents', CountryModel::NODE_AS_NONE)
->setResolution('currencies', \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_ALL)
->setResolution('languages', \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_ALL)
->setResolution('continents', \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_ALL)
->findByAlpha2('PT')
->toArray();
```
Expand Down Expand Up @@ -371,6 +370,31 @@ instead of:

The node resolutions works with the others models like "currencies", "languages", etc.

#### Node resolutions and immutability

When the resolution is changed it will be back to the previous state in the next model call.

Example:

```php
$iso = new ISOCodes();

echo $iso->countries()
->setResolution('currencies', \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_ALL)
->findByAlpha2('PT')
->currencies[0]; // Returns "Euro"

echo $iso->countries()
->findByAlpha2('PT')
->currencies[0]; // Returns "EUR"
```
In order to keep persistent the resolutions it's possible to pass the resolution values to the constructor. Example:

```php
$iso = new ISOCodes(new ISOCodes(defaultResolutions: [
'currencies' => \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_NAME
]);
```

## Custom dataset and locales

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A PHP library that provides ISO codes for countries, currencies and languages.",
"type": "library",
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-ctype": "*",
"illuminate/support": "^8.34.0|~v9.0|~v10.0"
},
Expand Down
24 changes: 24 additions & 0 deletions config/isocodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,28 @@
'continents' => \Juanparati\ISOCodes\Data\Continents\ContinentsEN::class,
],


/*
|--------------------------------------------------------------------------
| Default resolutions
|--------------------------------------------------------------------------
|
*/
'resolutions' => [
'currencies' => \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_CODE,
'continents' => \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_CODE,
'languages' => \Juanparati\ISOCodes\Enums\NodeResolution::NODE_AS_CODE,
],


/*
|--------------------------------------------------------------------------
| Additional options
|--------------------------------------------------------------------------
|
*/
'options' => [
'currencyAsNumber' => false // Specify if the currency code is returned as a number.
],

];
28 changes: 28 additions & 0 deletions src/Contracts/ISOModelContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Juanparati\ISOCodes\Contracts;

use Illuminate\Support\Collection;
use Juanparati\ISOCodes\Enums\NodeResolution;
use Juanparati\ISOCodes\ISOCodes;

interface ISOModelContract
Expand Down Expand Up @@ -30,4 +31,31 @@ public function all(bool $asArray = false): Collection;
* @return array
*/
public function list(): Collection;


/**
* Clone model.
*
* @return static
*/
public function clone(): static;


/**
* Set resolution.
*
* @param string $node
* @param NodeResolution $format
* @return $this
*/
public function setResolution(string $node, NodeResolution $format): static;


/**
* Set currency as number.
*
* @param bool $state
* @return $this
*/
public function setCurrencyAsNumber(bool $state): static;
}
11 changes: 11 additions & 0 deletions src/Enums/NodeResolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Juanparati\ISOCodes\Enums;

enum NodeResolution
{
case NODE_AS_CODE;
case NODE_AS_NAME;
case NODE_AS_ALL;
case NODE_AS_NONE;
}
41 changes: 39 additions & 2 deletions src/ISOCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Pluralizer;
use Juanparati\ISOCodes\Contracts\ISODataContract;
use Juanparati\ISOCodes\Contracts\ISOModelContract;
use Juanparati\ISOCodes\Enums\NodeResolution;
use Juanparati\ISOCodes\Exceptions\ISOModelNotFound;

/**
Expand Down Expand Up @@ -33,6 +34,28 @@ class ISOCodes
];


/**
* Default resolutions.
*
* @var array
*/
protected array $defaultResolutions = [
'currencies' => NodeResolution::NODE_AS_CODE,
'continents' => NodeResolution::NODE_AS_CODE,
'languages' => NodeResolution::NODE_AS_CODE,
];


/**
* Default options.
*
* @var array|false[]
*/
protected array $defaultOptions = [
'currencyAsNumber' => false
];


/**
* Loaded models.
*
Expand All @@ -53,10 +76,18 @@ class ISOCodes
* Constructor.
*
* @param array $datasets
* @param array $defaultResolutions
* @param array $defaultOptions
*/
public function __construct(array $datasets = [])
public function __construct(
array $datasets = [],
array $defaultResolutions = [],
array $defaultOptions = [],
)
{
$this->datasets = array_merge($this->datasets, $datasets);
$this->defaultResolutions = array_merge($this->defaultResolutions, $defaultResolutions);
$this->defaultOptions = array_merge($this->defaultOptions, $defaultOptions);
}


Expand All @@ -80,7 +111,13 @@ public function __call(string $name, array $args)
}
}

return $this->modelInstances[$model];

foreach ($this->defaultResolutions as $node => $resolution) {
$this->modelInstances[$model]->setResolution($node, $resolution);
}

return $this->modelInstances[$model]
->setCurrencyAsNumber($this->defaultOptions['currencyAsNumber'] ?? false);
}


Expand Down
39 changes: 22 additions & 17 deletions src/Models/BasicModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Juanparati\ISOCodes\Enums\NodeResolution;
use Juanparati\ISOCodes\Exceptions\ISONodeAttributeMissing;
use Juanparati\ISOCodes\ISOCodes;
use SebastianBergmann\CodeCoverage\Report\Xml\Node;

/**
* Base class used by the models.
Expand All @@ -15,24 +17,16 @@ abstract class BasicModelBase

use Macroable;

/**
* Subnode resolution formats.
*/
public const NODE_AS_CODE = 'code';
public const NODE_AS_NAME = 'name';
public const NODE_AS_ALL = 'all';
public const NODE_AS_NONE = 'none';


/**
* Define node resolution formats.
*
* @var array
* @var NodeResolution[]
*/
protected array $nodeResolution = [
'currencies' => self::NODE_AS_CODE,
'continents' => self::NODE_AS_CODE,
'languages' => self::NODE_AS_CODE,
'currencies' => NodeResolution::NODE_AS_CODE,
'continents' => NodeResolution::NODE_AS_CODE,
'languages' => NodeResolution::NODE_AS_CODE,
];


Expand Down Expand Up @@ -79,11 +73,11 @@ public function setCurrencyAsNumber(bool $state): static
* Set the node resolution.
*
* @param string $node
* @param string $format
* @param NodeResolution $format
* @return $this
* @throws ISONodeAttributeMissing
*/
public function setResolution(string $node, string $format = self::NODE_AS_CODE): static
public function setResolution(string $node, NodeResolution $format = NodeResolution::NODE_AS_CODE): static
{
if (!isset($this->nodeResolution[$node])) {
throw new ISONodeAttributeMissing('Node attribute is missing');
Expand Down Expand Up @@ -122,6 +116,17 @@ public function setOptions(array $options): static
}


/**
* Clone current model.
*
* @return $this
*/
public function clone() : static
{
return clone $this;
}


/**
* Resolve node data.
*
Expand All @@ -135,16 +140,16 @@ protected function resolveNodeData(
Collection $modelData,
array $codes,
): ?array {
if ($this->nodeResolution[$node] === self::NODE_AS_NONE) {
if ($this->nodeResolution[$node] === NodeResolution::NODE_AS_NONE) {
return null;
}

$list = [];

foreach ($codes as $code) {
if ($this->nodeResolution[$node] === self::NODE_AS_CODE) {
if ($this->nodeResolution[$node] === NodeResolution::NODE_AS_CODE) {
$list[] = $code;
} elseif ($this->nodeResolution[$node] === self::NODE_AS_NAME) {
} elseif ($this->nodeResolution[$node] === NodeResolution::NODE_AS_NAME) {
$list[] = $modelData[$code] ?? null;
} else {
$list[$code] = $modelData[$code] ?? null;
Expand Down
5 changes: 3 additions & 2 deletions src/Models/CountryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Juanparati\ISOCodes\Contracts\ISOModelContract;
use Juanparati\ISOCodes\Enums\NodeResolution;
use Juanparati\ISOCodes\Models\Extensions\CollectionMethodCallable;
use Juanparati\ISOCodes\Models\Extensions\NodeSearchable;

Expand Down Expand Up @@ -171,8 +172,8 @@ public function all(bool $asArray = false): Collection
} 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(),
NodeResolution::NODE_AS_CODE => array_map(fn ($cur) => (string) $currencyNumbers[$cur], $data),
NodeResolution::NODE_AS_ALL => collect($data)->mapWithKeys(fn ($name, $cur) => [(string) $currencyNumbers[$cur] => $name])->toArray(),
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/Providers/ISOCodesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public function register()
$this->mergeConfigFrom(__DIR__ . '/../../config/isocodes.php', 'isocodes');

$this->app->singleton(ISOCodes::class, function () {
return new ISOCodes($this->app['config']['isocodes']['datasets'] ?? []);
$config = $this->app['config']['isocodes'] ?? [];
return new ISOCodes(
$config['datasets'] ?? [],
$config['resolutions'] ?? [],
$config['options'] ?? []
);
});
}
}
Loading

0 comments on commit 95e21e9

Please sign in to comment.