-
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.
Add 'is_imei' validation rule (#141)
A new cell validation rule named `is_imei` has been added to check if a value is a valid International Mobile Equipment Identity (IMEI). This rule has been implemented in the schema-examples. Additionally, updated the documentation and "Plan to add" badge in README.md to reflect this new rule.
- Loading branch information
Showing
8 changed files
with
144 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,7 @@ columns: | |
is_iban: true | ||
is_bic: true | ||
postal_code: US | ||
is_imei: true | ||
|
||
is_version: true | ||
is_punct: true | ||
|
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,49 @@ | ||
<?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 IsImei extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[ | ||
self::DEFAULT => [ | ||
'true', | ||
'Validates an International Mobile Equipment Identity (IMEI). ' . | ||
'See: https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
if (!Validator::imei()->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid IMEI number."; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,73 @@ | ||
<?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\IsImei; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsImeiTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = IsImei::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame(null, $rule->validate('')); | ||
|
||
$valid = [ | ||
'', | ||
'35-209900-176148-1', | ||
'490154203237518', | ||
'35-007752-323751-3', | ||
'35-209900-176148-1', | ||
'350077523237513', | ||
'356938035643809', | ||
'490154203237518', | ||
]; | ||
|
||
foreach ($valid as $value) { | ||
isSame('', $rule->test($value), "\"{$value}\""); | ||
} | ||
|
||
$rule = $this->create(false); | ||
isSame(null, $rule->validate(' 1')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(true); | ||
|
||
$invalid = [ | ||
';', | ||
'!@#$%^&*()', | ||
'aBc 123', | ||
'aBc-123', | ||
'490154203237512', | ||
'4901542032375125', | ||
]; | ||
|
||
foreach ($invalid as $value) { | ||
isSame( | ||
"Value \"{$value}\" is not a valid IMEI number.", | ||
$rule->test($value), | ||
$value, | ||
); | ||
} | ||
} | ||
} |
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