-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced "charset" property in CSV schema examples for validating if the string is in a specific charset. Created a new Charset rule class and corresponding test to handle this validation. The readme and schemas are updated accordingly to reflect this new feature.
- Loading branch information
Showing
9 changed files
with
153 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?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 Charset extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
self::getHelpTitle(), | ||
[ | ||
self::DEFAULT => [ | ||
'charset_code', | ||
'Validates if a string is in a specific charset. Example: "UTF-8".', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
$charset = $this->getOptionAsString(); | ||
if ($charset === '') { | ||
return 'The charset is not specified.'; | ||
} | ||
|
||
if (!Validator::charset($charset)->validate($cellValue)) { | ||
return "The value \"<c>{$cellValue}</c>\" is not in the charset \"{$charset}\"."; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static function getHelpTitle(): array | ||
{ | ||
$maxOnLine = 10; | ||
$list = \mb_list_encodings(); | ||
\sort($list, \SORT_NATURAL); | ||
$lines = \array_chunk($list, $maxOnLine); | ||
|
||
$result = ['Check if a string is in a specific charset. Available charsets:']; | ||
foreach ($lines as $line) { | ||
$result[] = ' - ' . \implode(', ', $line); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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\Charset; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class CharsetTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = Charset::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create('UTF-8'); | ||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('USD')); | ||
isSame('', $rule->test('EUR')); | ||
|
||
$rule = $this->create('ASCII'); | ||
isSame('', $rule->test(\mb_convert_encoding('strawberry', 'ASCII'))); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create('ASCII'); | ||
isSame( | ||
'The value "日本国" is not in the charset "ASCII".', | ||
$rule->test('日本国'), | ||
); | ||
|
||
$rule = $this->create(''); | ||
isSame( | ||
'The charset is not specified.', | ||
$rule->test('日本国'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ columns: | |
is_card_number: true | ||
|
||
# Strings | ||
is_charset: true | ||
is_hex_rgb_color: true | ||
no_whitespace: true | ||
|
||
|