RoyalMailShippingProvider
extends AbstractShippingProvider
in package
Royal Mail Shipping Provider class.
Provides Royal Mail tracking number validation, supported countries, and tracking URL generation.
Table of Contents
- TRACKING_PATTERNS = array('GB' => array( // United Kingdom. 'patterns' => array( // UPU S10 international formats. '/^[A-Z]{2}\d{9}GB$/', // International format: XX#########GB. '/^[A-Z]{2}\d{7}GB$/', // Alternative international format: XX#######GB. // Domestic tracking formats. '/^[A-Z]{1}\d{9}[A-Z]{1}$/', // Domestic format: X#########X. '/^[A-Z]{2}\d{8}[A-Z]{2}$/', // Standard format: XX########XX. '/^[A-Z]{2}\d{6}[A-Z]{2}$/', // Compact format: XX######XX. // Service-specific patterns. '/^[A-Z]{4}\d{10}$/', // Special delivery format: XXXX##########. '/^SD\d{8,12}$/', // Signed For service. '/^SF\d{8,12}$/', // Special Delivery. '/^RM\d{8,12}$/', // Royal Mail standard. // Digital tracking formats. '/^\d{16}$/', // 16-digit returns label or digital. '/^\d{14}$/', // 14-digit returns label. '/^\d{13}$/', // 13-digit domestic/international tracking. '/^\d{12}$/', // 12-digit domestic tracking. '/^\d{11}$/', // 11-digit domestic tracking. '/^\d{10}$/', // 10-digit legacy Parcelforce/RM. '/^\d{9}$/', // 9-digit legacy Parcelforce/RM. // Parcelforce (Royal Mail Group). '/^PF\d{8,12}$/', // Parcelforce Express. '/^[A-Z]{2}\d{8}PF$/', // Parcelforce International. '/^\d{13}$/', // Parcelforce Worldwide numeric. // International tracked services. '/^IT\d{9}GB$/', // International Tracked. '/^IE\d{9}GB$/', // International Economy. '/^IS\d{9}GB$/', // International Standard. // Business services. '/^BF\d{8,12}$/', // Business services. '/^[A-Z]{3}\d{8,12}$/', // Three-letter business codes. // Legacy formats. '/^[A-Z]{1}\d{8}[A-Z]{2}$/', // Legacy format: X########XX. '/^[0-9]{9}[A-Z]{3}$/', ), 'confidence' => 80, ))
- Royal Mail tracking number patterns with enhanced service detection.
- can_ship_from() : bool
- Check if the shipping provider can ship from a specific country.
- can_ship_from_to() : bool
- Check if the shipping provider can ship from a specific country to another.
- can_ship_to() : bool
- Check if the shipping provider can ship to a specific country.
- get_icon() : string
- Get the icon of the shipping provider.
- get_key() : string
- Get the key of the shipping provider.
- get_name() : string
- Get the name of the shipping provider.
- get_shipping_from_countries() : array<string|int, mixed>
- Get the countries this shipping provider can ship from.
- get_shipping_to_countries() : array<string|int, mixed>
- Get the countries this shipping provider can ship to.
- get_tracking_url() : string
- Get the tracking URL for a given tracking number.
- try_parse_tracking_number() : array<string|int, mixed>|null
- Try to parse a Royal Mail tracking number.
- validate_country_pattern() : bool
- Validate tracking number against country-specific patterns.
Constants
TRACKING_PATTERNS
Royal Mail tracking number patterns with enhanced service detection.
private
array<string, array{patterns: array, confidence: int}>
TRACKING_PATTERNS
= array('GB' => array(
// United Kingdom.
'patterns' => array(
// UPU S10 international formats.
'/^[A-Z]{2}\d{9}GB$/',
// International format: XX#########GB.
'/^[A-Z]{2}\d{7}GB$/',
// Alternative international format: XX#######GB.
// Domestic tracking formats.
'/^[A-Z]{1}\d{9}[A-Z]{1}$/',
// Domestic format: X#########X.
'/^[A-Z]{2}\d{8}[A-Z]{2}$/',
// Standard format: XX########XX.
'/^[A-Z]{2}\d{6}[A-Z]{2}$/',
// Compact format: XX######XX.
// Service-specific patterns.
'/^[A-Z]{4}\d{10}$/',
// Special delivery format: XXXX##########.
'/^SD\d{8,12}$/',
// Signed For service.
'/^SF\d{8,12}$/',
// Special Delivery.
'/^RM\d{8,12}$/',
// Royal Mail standard.
// Digital tracking formats.
'/^\d{16}$/',
// 16-digit returns label or digital.
'/^\d{14}$/',
// 14-digit returns label.
'/^\d{13}$/',
// 13-digit domestic/international tracking.
'/^\d{12}$/',
// 12-digit domestic tracking.
'/^\d{11}$/',
// 11-digit domestic tracking.
'/^\d{10}$/',
// 10-digit legacy Parcelforce/RM.
'/^\d{9}$/',
// 9-digit legacy Parcelforce/RM.
// Parcelforce (Royal Mail Group).
'/^PF\d{8,12}$/',
// Parcelforce Express.
'/^[A-Z]{2}\d{8}PF$/',
// Parcelforce International.
'/^\d{13}$/',
// Parcelforce Worldwide numeric.
// International tracked services.
'/^IT\d{9}GB$/',
// International Tracked.
'/^IE\d{9}GB$/',
// International Economy.
'/^IS\d{9}GB$/',
// International Standard.
// Business services.
'/^BF\d{8,12}$/',
// Business services.
'/^[A-Z]{3}\d{8,12}$/',
// Three-letter business codes.
// Legacy formats.
'/^[A-Z]{1}\d{8}[A-Z]{2}$/',
// Legacy format: X########XX.
'/^[0-9]{9}[A-Z]{3}$/',
),
'confidence' => 80,
))
Methods
can_ship_from()
Check if the shipping provider can ship from a specific country.
public
can_ship_from(string $country_code) : bool
Parameters
- $country_code : string
-
The country code to check.
Return values
bool — True if the provider can ship from the country, false otherwise.can_ship_from_to()
Check if the shipping provider can ship from a specific country to another.
public
can_ship_from_to(string $shipping_from, string $shipping_to) : bool
Parameters
- $shipping_from : string
-
The country code from which the shipment is sent.
- $shipping_to : string
-
The country code to which the shipment is sent.
Return values
bool — True if the provider can ship from the source to the destination, false otherwise.can_ship_to()
Check if the shipping provider can ship to a specific country.
public
can_ship_to(string $country_code) : bool
Parameters
- $country_code : string
-
The country code to check.
Return values
bool — True if the provider can ship to the country, false otherwise.get_icon()
Get the icon of the shipping provider.
public
get_icon() : string
Return values
string —get_key()
Get the key of the shipping provider.
public
get_key() : string
Return values
string —get_name()
Get the name of the shipping provider.
public
get_name() : string
Return values
string —get_shipping_from_countries()
Get the countries this shipping provider can ship from.
public
get_shipping_from_countries() : array<string|int, mixed>
Return values
array<string|int, mixed> — List of country codes.get_shipping_to_countries()
Get the countries this shipping provider can ship to.
public
get_shipping_to_countries() : array<string|int, mixed>
Royal Mail ships internationally, so we return a comprehensive list.
Return values
array<string|int, mixed> — List of country codes.get_tracking_url()
Get the tracking URL for a given tracking number.
public
get_tracking_url(string $tracking_number) : string
Parameters
- $tracking_number : string
-
The tracking number.
Return values
string — The tracking URL.try_parse_tracking_number()
Try to parse a Royal Mail tracking number.
public
try_parse_tracking_number(string $tracking_number, string $shipping_from, string $shipping_to) : array<string|int, mixed>|null
Parameters
- $tracking_number : string
-
The tracking number to parse.
- $shipping_from : string
-
The country code of the shipping origin.
- $shipping_to : string
-
The country code of the shipping destination.
Return values
array<string|int, mixed>|null — An array with 'url' and 'ambiguity_score' if valid, null otherwise.validate_country_pattern()
Validate tracking number against country-specific patterns.
private
validate_country_pattern(string $tracking_number, string $country_code) : bool
Parameters
- $tracking_number : string
-
The tracking number to validate.
- $country_code : string
-
The country code for the shipment.
