• 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

83.33
/src/Formatter/CAFormatter.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 substr;
11

12
/**
13
 * Validates and formats postcodes in Canada.
14
 *
15
 * The format is ANA NAN, where A is a letter and N is a digit.
16
 * Postal codes do not include the letters D, F, I, O, Q or U.
17
 * The first position also does not make use of the letters W or Z.
18
 *
19
 * @see https://en.wikipedia.org/wiki/List_of_postal_codes
20
 * @see https://en.wikipedia.org/wiki/Postal_codes_in_Canada
21
 */
22
final class CAFormatter implements CountryPostcodeFormatter
23
{
24
    public function hint(): string
25
    {
NEW
26
        return 'The format is ANA NAN, where A is a letter and N is a digit.';
×
27
    }
28

29
    public function format(string $postcode): ?string
30
    {
31
        if (preg_match('/^([ABCEGHJ-NPRSTV-Z][0-9]){3}$/', $postcode) !== 1) {
91✔
32
            return null;
32✔
33
        }
34

35
        if ($postcode[0] === 'W' || $postcode[0] === 'Z') {
59✔
36
            return null;
2✔
37
        }
38

39
        return substr($postcode, 0, 3) . ' ' . substr($postcode, 3);
57✔
40
    }
41
}
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