• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / postcode / 19098923587

05 Nov 2025 10:27AM UTC coverage: 81.782% (-17.6%) from 99.396%
19098923587

Pull #19

github

web-flow
Merge 79bf98178 into 8ef37e5aa
Pull Request #19: Add hint to formatter, so a hint can be provided if postcode is invalid

5 of 184 new or added lines in 183 files covered. (2.72%)

2 existing lines in 1 file now uncovered.

826 of 1010 relevant lines covered (81.78%)

15.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/Formatter/VIFormatter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Postcode\Formatter;
6

7
use Brick\Postcode\CountryPostcodeFormatter;
8

9
use function preg_match;
10
use function strlen;
11
use function substr;
12

13
/**
14
 * Validates and formats postcodes in U.S. Virgin Islands.
15
 *
16
 * U.S. ZIP codes. Range 00801 - 00851.
17
 *
18
 * @see https://en.wikipedia.org/wiki/List_of_postal_codes
19
 */
20
final class VIFormatter implements CountryPostcodeFormatter
21
{
22
    public function hint(): string
23
    {
NEW
24
        return 'U.S. ZIP codes. Range 00801 - 00851.';
×
25
    }
26

27
    public function format(string $postcode): ?string
28
    {
29
        if (preg_match('/^[0-9]+$/', $postcode) !== 1) {
32✔
30
            return null;
11✔
31
        }
32

33
        $length = strlen($postcode);
21✔
34

35
        if ($length === 5) {
21✔
36
            $zip = $postcode;
5✔
37
        } elseif ($length === 9) {
16✔
38
            $zip = substr($postcode, 0, 5);
5✔
39
        } else {
40
            return null;
11✔
41
        }
42

43
        if ($zip < '00801' || $zip > '00851') {
10✔
44
            return null;
6✔
45
        }
46

47
        if ($length === 5) {
4✔
48
            return $postcode;
2✔
49
        }
50

51
        return $zip . '-' . substr($postcode, 5);
2✔
52
    }
53
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc