|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 3 | +SPDX-License-Identifier: ISC |
| 4 | +SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> |
| 5 | +--> |
| 6 | + |
| 7 | +# CreditCardFormatter |
| 8 | + |
| 9 | +The `CreditCardFormatter` formats credit card numbers with automatic card type detection. It supports major card types including Visa, MasterCard, American Express, Discover, and JCB. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Basic Usage with Auto-Detection |
| 14 | + |
| 15 | +```php |
| 16 | +use Respect\StringFormatter\CreditCardFormatter; |
| 17 | + |
| 18 | +$formatter = new CreditCardFormatter(); |
| 19 | + |
| 20 | +echo $formatter->format('4123456789012345'); |
| 21 | +// Outputs: "4123 4567 8901 2345" (Visa detected) |
| 22 | + |
| 23 | +echo $formatter->format('371234567890123'); |
| 24 | +// Outputs: "3712 345678 90123" (Amex, different pattern) |
| 25 | + |
| 26 | +echo $formatter->format('5112345678901234'); |
| 27 | +// Outputs: "5112 3456 7890 1234" (MasterCard detected) |
| 28 | +``` |
| 29 | + |
| 30 | +### Input Cleaning |
| 31 | + |
| 32 | +The formatter automatically removes non-digit characters from the input: |
| 33 | + |
| 34 | +```php |
| 35 | +use Respect\StringFormatter\CreditCardFormatter; |
| 36 | + |
| 37 | +$formatter = new CreditCardFormatter(); |
| 38 | + |
| 39 | +echo $formatter->format('4123-4567-8901-2345'); |
| 40 | +// Outputs: "4123 4567 8901 2345" |
| 41 | + |
| 42 | +echo $formatter->format('4123 4567 8901 2345'); |
| 43 | +// Outputs: "4123 4567 8901 2345" |
| 44 | + |
| 45 | +echo $formatter->format('4123.4567.8901.2345'); |
| 46 | +// Outputs: "4123 4567 8901 2345" |
| 47 | +``` |
| 48 | + |
| 49 | +### Custom Pattern |
| 50 | + |
| 51 | +You can specify a custom pattern to override auto-detection: |
| 52 | + |
| 53 | +```php |
| 54 | +use Respect\StringFormatter\CreditCardFormatter; |
| 55 | + |
| 56 | +$formatter = new CreditCardFormatter('####-####-####-####'); |
| 57 | + |
| 58 | +echo $formatter->format('4123456789012345'); |
| 59 | +// Outputs: "4123-4567-8901-2345" |
| 60 | + |
| 61 | +$formatterCompact = new CreditCardFormatter('################'); |
| 62 | + |
| 63 | +echo $formatterCompact->format('4123456789012345'); |
| 64 | +// Outputs: "4123456789012345" |
| 65 | +``` |
| 66 | + |
| 67 | +## API |
| 68 | + |
| 69 | +### `CreditCardFormatter::__construct` |
| 70 | + |
| 71 | +- `__construct(?string $pattern = null)` |
| 72 | + |
| 73 | +Creates a new credit card formatter instance. |
| 74 | + |
| 75 | +**Parameters:** |
| 76 | + |
| 77 | +- `$pattern`: Custom format pattern or null for auto-detection (default: null) |
| 78 | + |
| 79 | +**If null**: The formatter automatically detects the card type and applies the appropriate pattern |
| 80 | + |
| 81 | +**If provided**: Uses the specified pattern for all cards |
| 82 | + |
| 83 | +### `format` |
| 84 | + |
| 85 | +- `format(string $input): string` |
| 86 | + |
| 87 | +Formats the input credit card number. |
| 88 | + |
| 89 | +**Parameters:** |
| 90 | + |
| 91 | +- `$input`: The credit card number (can include spaces, dashes, dots, etc.) |
| 92 | + |
| 93 | +**Returns:** The formatted credit card number |
| 94 | + |
| 95 | +## Auto-Detection |
| 96 | + |
| 97 | +The formatter automatically detects card type based on prefix and length: |
| 98 | + |
| 99 | +| Card Type | Prefix Ranges | Length | Format Pattern | |
| 100 | +| -------------------- | ----------------- | ---------- | --------------------------------------- | |
| 101 | +| **Visa** | 4 | 13, 16, 19 | `#### #### #### ####` | |
| 102 | +| **MasterCard** | 51-55 | 16 | `#### #### #### ####` | |
| 103 | +| **American Express** | 34, 37 | 15 | `#### ########## ######` - 4-6-5 format | |
| 104 | +| **Discover** | 6011, 644-649, 65 | 16 | `#### #### #### ####` | |
| 105 | +| **JCB** | 3528-3589 | 16 | `#### #### #### ####` | |
| 106 | +| **Unknown** | (any) | any | `#### #### #### ####` - default pattern | |
| 107 | + |
| 108 | +## Examples |
| 109 | + |
| 110 | +| Input | Output | Card Type | |
| 111 | +| --------------------- | --------------------- | -------------- | |
| 112 | +| `4123456789012345` | `4123 4567 8901 2345` | Visa | |
| 113 | +| `5112345678901234` | `5112 3456 7890 1234` | MasterCard | |
| 114 | +| `341234567890123` | `3412 345678 90123` | Amex | |
| 115 | +| `371234567890123` | `3712 345678 90123` | Amex | |
| 116 | +| `6011000990139424` | `6011 0009 9013 9424` | Discover | |
| 117 | +| `3528000012345678` | `3528 0000 1234 5678` | JCB | |
| 118 | +| `1234567890123456` | `1234 5678 9012 3456` | Unknown | |
| 119 | +| `4123-4567-8901-2345` | `4123 4567 8901 2345` | Visa (cleaned) | |
| 120 | +| `4123 4567 8901 2345` | `4123 4567 8901 2345` | Visa (cleaned) | |
| 121 | + |
| 122 | +## Notes |
| 123 | + |
| 124 | +- Non-digit characters are automatically removed from the input |
| 125 | +- Card type detection is based on card prefix and length (not Luhn validation) |
| 126 | +- If card type cannot be determined, uses the default pattern `#### #### #### ####` |
| 127 | +- Uses `PatternFormatter` internally for formatting |
| 128 | +- Empty strings return empty strings |
| 129 | +- Numbers longer than the pattern aretruncated to fit the pattern |
| 130 | +- Custom patterns follow `PatternFormatter` syntax (use `#` for digits) |
0 commit comments