|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 3 | +SPDX-License-Identifier: ISC |
| 4 | +SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> |
| 5 | +--> |
| 6 | + |
| 7 | +# SecureCreditCardFormatter |
| 8 | + |
| 9 | +The `SecureCreditCardFormatter` formats and masks credit card numbers for secure display. It automatically detects card types, formats them appropriately, and masks sensitive portions. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Basic Usage |
| 14 | + |
| 15 | +```php |
| 16 | +use Respect\StringFormatter\SecureCreditCardFormatter; |
| 17 | + |
| 18 | +$formatter = new SecureCreditCardFormatter(); |
| 19 | + |
| 20 | +echo $formatter->format('4123456789012345'); |
| 21 | +// Outputs: "4123 **** **** 2345" (Visa) |
| 22 | + |
| 23 | +echo $formatter->format('341234567890123'); |
| 24 | +// Outputs: "3412 ****** 90123" (Amex, 4-6-5 format) |
| 25 | + |
| 26 | +echo $formatter->format('5112345678901234'); |
| 27 | +// Outputs: "5112 **** **** 1234" (MasterCard) |
| 28 | + |
| 29 | +echo $formatter->format('36123456789012'); |
| 30 | +// Outputs: "3612 ****** 9012" (Diners Club, 4-6-4 format) |
| 31 | + |
| 32 | +echo $formatter->format('4123456789012345678'); |
| 33 | +// Outputs: "4123 **** **** **** 678" (Visa 19-digit) |
| 34 | +``` |
| 35 | + |
| 36 | +### Custom Mask Character |
| 37 | + |
| 38 | +```php |
| 39 | +use Respect\StringFormatter\SecureCreditCardFormatter; |
| 40 | + |
| 41 | +$formatter = new SecureCreditCardFormatter('X'); |
| 42 | + |
| 43 | +echo $formatter->format('4123456789012345'); |
| 44 | +// Outputs: "4123 XXXX XXXX 2345" |
| 45 | +``` |
| 46 | + |
| 47 | +### Input Cleaning |
| 48 | + |
| 49 | +The formatter automatically removes non-digit characters from the input: |
| 50 | + |
| 51 | +```php |
| 52 | +use Respect\StringFormatter\SecureCreditCardFormatter; |
| 53 | + |
| 54 | +$formatter = new SecureCreditCardFormatter(); |
| 55 | + |
| 56 | +echo $formatter->format('4123-4567-8901-2345'); |
| 57 | +// Outputs: "4123 **** **** 2345" |
| 58 | +``` |
| 59 | + |
| 60 | +## API |
| 61 | + |
| 62 | +### `SecureCreditCardFormatter::__construct` |
| 63 | + |
| 64 | +- `__construct(string $maskChar = '*')` |
| 65 | + |
| 66 | +Creates a new secret credit card formatter instance. |
| 67 | + |
| 68 | +**Parameters:** |
| 69 | + |
| 70 | +- `$maskChar`: Character to use for masking (default: '\*') |
| 71 | + |
| 72 | +### `format` |
| 73 | + |
| 74 | +- `format(string $input): string` |
| 75 | + |
| 76 | +Formats and masks the input credit card number. |
| 77 | + |
| 78 | +**Parameters:** |
| 79 | + |
| 80 | +- `$input`: The credit card number (can include spaces, dashes, dots, etc.) |
| 81 | + |
| 82 | +**Returns:** The formatted and masked credit card number |
| 83 | + |
| 84 | +## Masking |
| 85 | + |
| 86 | +The formatter applies masking after formatting to ensure predictable positions: |
| 87 | + |
| 88 | +| Card Type | Example Input | Mask Range | Output | |
| 89 | +| -------------------- | --------------------- | ----------------- | ------------------------- | |
| 90 | +| **Visa** (16) | `4123456789012345` | `6-9,11-14` | `4123 **** **** 2345` | |
| 91 | +| **Visa** (19) | `4123456789012345678` | `6-9,11-14,16-19` | `4123 **** **** **** 678` | |
| 92 | +| **MasterCard** | `5112345678901234` | `6-9,11-14` | `5112 **** **** 1234` | |
| 93 | +| **American Express** | `341234567890123` | `6-11` | `3412 ****** 90123` | |
| 94 | +| **Discover** | `6011000990139424` | `6-9,11-14` | `6011 **** **** 9424` | |
| 95 | +| **JCB** | `3528000012345678` | `6-9,11-14` | `3528 **** **** 5678` | |
| 96 | +| **Diners Club** (14) | `36123456789012` | `6-11` | `3612 ****** 9012` | |
| 97 | +| **UnionPay** | `6212345678901234` | `6-9,11-14` | `6212 **** **** 1234` | |
| 98 | +| **RuPay** | `8112345678901234` | `6-9,11-14` | `8112 **** **** 1234` | |
| 99 | + |
| 100 | +## Examples |
| 101 | + |
| 102 | +| Input | Output | Card Type | |
| 103 | +| --------------------- | ------------------------- | ------------ | |
| 104 | +| `4123456789012345` | `4123 **** **** 2345` | Visa | |
| 105 | +| `4123456789012345678` | `4123 **** **** **** 678` | Visa (19) | |
| 106 | +| `5112345678901234` | `5112 **** **** 1234` | MasterCard | |
| 107 | +| `341234567890123` | `3412 ****** 90123` | Amex | |
| 108 | +| `371234567890123` | `3712 ****** 90123` | Amex | |
| 109 | +| `6011000990139424` | `6011 **** **** 9424` | Discover | |
| 110 | +| `3528000012345678` | `3528 **** **** 5678` | JCB | |
| 111 | +| `36123456789012` | `3612 ****** 9012` | Diners Club | |
| 112 | +| `6212345678901234` | `6212 **** **** 1234` | UnionPay | |
| 113 | +| `8112345678901234` | `8112 **** **** 1234` | RuPay | |
| 114 | +| `4123-4567-8901-2345` | `4123 **** **** 2345` | Visa (clean) | |
| 115 | + |
| 116 | +## Notes |
| 117 | + |
| 118 | +- Composes `CreditCardFormatter` for formatting and `MaskFormatter` for masking |
| 119 | +- Formats the card number first, then applies masking to the formatted string |
| 120 | +- Mask ranges are applied to 1-based positions in the formatted string |
| 121 | +- Non-digit characters are automatically removed from input |
| 122 | +- Inputs with fewer than 9 digits are returned as cleaned digits without formatting or masking |
| 123 | +- Uses `CreditCardFormatter` for card type detection and formatting |
0 commit comments