-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add shifted number-row digit correction support #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a2d978c
3def88c
af55d1c
4dba702
7332274
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,28 @@ final class PostcodeRules | |
| private const FORBIDDEN_FIRST_OUTWARD_LETTERS = 'QVX'; | ||
| private const FORBIDDEN_SECOND_OUTWARD_LETTERS = 'IJZ'; | ||
| private const AA9A_ALLOWED_FINAL_LETTERS = 'ABEHMNPRVWXY'; | ||
| private const SHIFTED_DIGIT_SYMBOLS = '!@"#$%^&*()'; | ||
| private const SHIFTED_DIGIT_ALIASES = [ | ||
| "\u{00A3}" => '#', | ||
| ]; | ||
|
|
||
| /** | ||
| * @var array<string, string> | ||
| */ | ||
| private const SHIFTED_DIGIT_TO_DIGIT = [ | ||
| '!' => '1', | ||
| '@' => '2', | ||
| '"' => '2', | ||
| '#' => '3', | ||
| "\u{00A3}" => '3', | ||
| '$' => '4', | ||
| '%' => '5', | ||
| '^' => '6', | ||
| '&' => '7', | ||
| '*' => '8', | ||
| '(' => '9', | ||
| ')' => '0', | ||
| ]; | ||
|
|
||
| /** | ||
| * @var array<string, list<string>> | ||
|
|
@@ -172,9 +194,31 @@ final class PostcodeRules | |
| public static function compactFromInput(string $input): string | ||
| { | ||
| $normalized = strtoupper($input); | ||
| $compact = preg_replace('/[^A-Z0-9]+/', '', $normalized); | ||
| $normalized = strtr($normalized, self::SHIFTED_DIGIT_ALIASES); | ||
| $compact = preg_replace('/[^A-Z0-9!@"#$%\^&*()]+/', '', $normalized); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Keeping shifted symbols in Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in |
||
| if ($compact === null || $compact === '') { | ||
| return ''; | ||
| } | ||
|
|
||
| return $compact ?? ''; | ||
| // Shifted symbols can validly stand in for digits, but never at postcode boundaries. | ||
| $compact = trim($compact, self::SHIFTED_DIGIT_SYMBOLS); | ||
|
|
||
| return $compact; | ||
| } | ||
|
|
||
| public static function containsDigitLikeCharacter(string $compact): bool | ||
| { | ||
| return strpbrk($compact, '0123456789' . self::SHIFTED_DIGIT_SYMBOLS) !== false; | ||
| } | ||
|
|
||
| public static function shiftedDigitReplacement(string $character): ?string | ||
| { | ||
| return self::SHIFTED_DIGIT_TO_DIGIT[$character] ?? null; | ||
| } | ||
|
|
||
| public static function stripShiftedDigitSymbols(string $compact): string | ||
| { | ||
| return str_replace(str_split(self::SHIFTED_DIGIT_SYMBOLS), '', $compact); | ||
| } | ||
|
|
||
| public static function displayFromCompact(string $compact): string | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Retaining
!@"#$%^&*()incompactFromInputcauses previously valid inputs with trailing/leading punctuation to be rejected. For example,EC1A 1AL!now compacts toEC1A1AL!(length 8), so analysis returns no candidate, whereas the same input was normalized toEC1A1ALbefore this change. Because these characters are common as sentence punctuation or wrappers, this introduces a real regression in format acceptance for otherwise valid postcodes.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, good catch. Addressed in
4dba702by trimming shifted symbols only at compact-string boundaries so surrounding punctuation/wrappers are ignored again (EC1A 1AL!,!EC1A 1AL,(EC1A 1AL)), while preserving in-position shifted symbols for deterministic digit correction. Added regression tests intests/CikmovTest.phpandtests/PostcodeRulesTest.php. Full suite passes:composer test->OK (140 tests, 1165 assertions).