# woo-postnl/4.4.1/vendor/myparcelnl/sdk/src/Helper/ValidatePostalCode.php

PostNL for WooCommerce, version 4.4.1. 41 lines.

- Page: https://pluginprobe.com/plugins/woo-postnl/4.4.1/code/vendor/myparcelnl/sdk/src/Helper/ValidatePostalCode.php
- Raw: https://pluginprobe.com/plugins/woo-postnl/4.4.1/raw/vendor/myparcelnl/sdk/src/Helper/ValidatePostalCode.php
- Modified: 2021-05-11T12:49:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/woo-postnl/4.4.1/code/vendor/myparcelnl/sdk/src/Helper/ValidatePostalCode.php#L10-L20`.

```php
<?php declare(strict_types=1);

namespace MyParcelNL\Sdk\src\Helper;

use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;

class ValidatePostalCode
{
    const VALIDATE_POSTAL_CODE_REGEX_NL = '/^[1-9]\d{3}\s?[a-z]{2}$/i';
    const VALIDATE_POSTAL_CODE_REGEX_BE = '/[1-9]\d{3}$/';

    public static function validate(string $postalCode, ?string $destinationCountry): bool
    {
        $validatePostalCode = ValidatePostalCode::getPostalCodeRegexByCountry($destinationCountry);

        if ($validatePostalCode) {
            return (bool) preg_match($validatePostalCode, $postalCode);
        }

        return true;
    }

    /**
     * @param string $local
     * @param string $destination
     *
     * @return string
     */
    public static function getPostalCodeRegexByCountry(string $destination): ?string
    {
        switch ($destination) {
            case AbstractConsignment::CC_NL:
                return self::VALIDATE_POSTAL_CODE_REGEX_NL;
            case AbstractConsignment::CC_BE:
                return self::VALIDATE_POSTAL_CODE_REGEX_BE;
            default:
                return null;
        }
    }
}

```
