Skip to content

Commit

Permalink
Add 'is_currency_code' rule (#46)
Browse files Browse the repository at this point in the history
A new rule 'is_currency_code' has been added to the schema examples in
full.json, full.php, and full.yml files. This rule validates ISO 4217
currency codes like 'GBP' or 'EUR'. Corresponding to this, a new PHP
file 'IsCurrencyCode.php' has been created under the 'src/Rules/Cell'
directory to implement the validation rule. Similarly, a new test file
'CurrencyCodeTest.php' has been added to check the proper functioning of
the newly implemented rule. The README.md file has been updated to
reflect these changes.
  • Loading branch information
SmetDenis committed Mar 17, 2024
1 parent c55358c commit cfb6a6f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ columns:
is_domain: true # Only domain name. Example: "example.com".
is_uuid: true # Only UUID4 format. Example: "550e8400-e29b-41d4-a716-446655440000".
is_alias: true # Only alias format. Example: "my-alias-123". It can contain letters, numbers, and dashes.
is_currency_code: true # Validates an ISO 4217 currency code like GBP or EUR. Case-sensitive. See: https://en.wikipedia.org/wiki/ISO_4217.

# Geography
is_latitude: true # Can be integer or float. Example: 50.123456.
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"is_domain" : true,
"is_uuid" : true,
"is_alias" : true,
"is_currency_code" : true,

"is_latitude" : true,
"is_longitude" : true,
Expand Down
15 changes: 8 additions & 7 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@
'date_format' => 'Y-m-d',
'is_date' => true,

'is_bool' => true,
'is_ip4' => true,
'is_url' => true,
'is_email' => true,
'is_domain' => true,
'is_uuid' => true,
'is_alias' => true,
'is_bool' => true,
'is_ip4' => true,
'is_url' => true,
'is_email' => true,
'is_domain' => true,
'is_uuid' => true,
'is_alias' => true,
'is_currency_code' => true,

'is_latitude' => true,
'is_longitude' => true,
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ columns:
is_domain: true # Only domain name. Example: "example.com".
is_uuid: true # Only UUID4 format. Example: "550e8400-e29b-41d4-a716-446655440000".
is_alias: true # Only alias format. Example: "my-alias-123". It can contain letters, numbers, and dashes.
is_currency_code: true # Validates an ISO 4217 currency code like GBP or EUR. Case-sensitive. See: https://en.wikipedia.org/wiki/ISO_4217.

# Geography
is_latitude: true # Can be integer or float. Example: 50.123456.
Expand Down
43 changes: 43 additions & 0 deletions src/Rules/Cell/IsCurrencyCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Rules\Cell;

use Respect\Validation\Validator;

final class IsCurrencyCode extends AbstractCellRule
{
protected const HELP_OPTIONS = [
self::DEFAULT => [
'true',
'Validates an ISO 4217 currency code like GBP or EUR. Case-sensitive. ' .
'See: https://en.wikipedia.org/wiki/ISO_4217',
],
];

public function validateRule(string $cellValue): ?string
{
if ($cellValue === '') {
return null;
}

if (!Validator::currencyCode()->validate($cellValue)) {
return "Value \"<c>{$cellValue}</c>\" is not a valid currency code (ISO_4217)";
}

return null;
}
}
51 changes: 51 additions & 0 deletions tests/Rules/Cell/CurrencyCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit\Rules\Cell;

use JBZoo\CsvBlueprint\Rules\Cell\IsCurrencyCode;
use JBZoo\PHPUnit\Rules\AbstractCellRule;

use function JBZoo\PHPUnit\isSame;

final class CurrencyCodeTest extends AbstractCellRule
{
protected string $ruleClass = IsCurrencyCode::class;

public function testPositive(): void
{
$rule = $this->create(true);
isSame('', $rule->test(''));
isSame('', $rule->test('USD'));
isSame('', $rule->test('EUR'));

$rule = $this->create(false);
isSame(null, $rule->validate('qwerty'));
}

public function testNegative(): void
{
$rule = $this->create(true);
isSame(
'Value "usd" is not a valid currency code (ISO_4217)',
$rule->test('usd'),
);
isSame(
'Value "qwerty" is not a valid currency code (ISO_4217)',
$rule->test('qwerty'),
);
}
}

0 comments on commit cfb6a6f

Please sign in to comment.