-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from TheDragonCode/1.x
Added ability to generate and validate alphanumeric numbers
- Loading branch information
Showing
17 changed files
with
423 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\CardNumber\Cards; | ||
|
||
use DragonCode\CardNumber\Concerns\Charsable; | ||
|
||
use function array_keys; | ||
use function array_values; | ||
use function implode; | ||
use function preg_match; | ||
use function preg_replace; | ||
use function str_replace; | ||
|
||
class Chars extends Card | ||
{ | ||
use Charsable; | ||
|
||
protected static array $numberLength = []; | ||
|
||
public static function isValid(int|string $cardNumber): bool | ||
{ | ||
$number = static::clear($cardNumber); | ||
|
||
return static::isValidLength($number) | ||
&& static::isValidChars($number) | ||
&& static::isValidPattern($number) | ||
&& static::isValidNumber($number); | ||
} | ||
|
||
protected static function isValidNumber(string $number): bool | ||
{ | ||
return parent::isValidNumber( | ||
str_replace(array_values(static::$chars), array_keys(static::$chars), $number) | ||
); | ||
} | ||
|
||
protected static function clear(int|string $number): string | ||
{ | ||
return preg_replace('/[^\w]/', '', static::lower($number)); | ||
} | ||
|
||
protected static function isValidChars(string $number): bool | ||
{ | ||
$chars = static::charsPattern(); | ||
|
||
return ! preg_match("/[^$chars\\d]/", $number); | ||
} | ||
|
||
protected static function charsPattern(): string | ||
{ | ||
return implode('', array_values(static::$chars)); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\CardNumber\Concerns; | ||
|
||
trait Charsable | ||
{ | ||
protected static array $chars = [ | ||
0 => 'f', | ||
1 => 'a', | ||
2 => 'e', | ||
3 => 'k', | ||
4 => 'n', | ||
5 => 'o', | ||
6 => 's', | ||
7 => 'x', | ||
8 => 'y', | ||
9 => 'z', | ||
]; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\CardNumber\Formatters; | ||
|
||
use DragonCode\CardNumber\Concerns\Charsable; | ||
|
||
use function array_keys; | ||
use function array_values; | ||
use function str_replace; | ||
|
||
class LoyaltyCharFormatter extends Formatter | ||
{ | ||
use Charsable; | ||
|
||
protected function pretty(string $number, int $length): string | ||
{ | ||
return parent::pretty(static::upper($this->encode($number)), $length); | ||
} | ||
|
||
protected function encode(string $value): string | ||
{ | ||
return str_replace(array_keys(static::$chars), array_values(static::$chars), $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
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
Oops, something went wrong.