Exception
8 months ago
Guards.php
8 months ago
ISO3166.php
8 months ago
ISO3166DataProvider.php
8 months ago
ISO3166DataValidator.php
8 months ago
ISO3166WithAliases.php
8 months ago
Guards.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /* |
| 6 | * (c) Rob Bast <rob.bast@gmail.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view |
| 9 | * the LICENSE file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Automattic\WooCommerce\Vendor\League\ISO3166; |
| 13 | |
| 14 | use Automattic\WooCommerce\Vendor\League\ISO3166\Exception\DomainException; |
| 15 | |
| 16 | final class Guards |
| 17 | { |
| 18 | /** |
| 19 | * Assert that input is not an empty string. |
| 20 | * |
| 21 | * @throws \Automattic\WooCommerce\Vendor\League\ISO3166\Exception\DomainException if input is an empty string |
| 22 | */ |
| 23 | public static function guardAgainstInvalidName(string $name): void |
| 24 | { |
| 25 | if ('' === trim($name)) { |
| 26 | throw new DomainException('Expected string, got empty string'); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Assert that input looks like an alpha2 key. |
| 32 | * |
| 33 | * @throws \Automattic\WooCommerce\Vendor\League\ISO3166\Exception\DomainException if input does not look like an alpha2 key |
| 34 | */ |
| 35 | public static function guardAgainstInvalidAlpha2(string $alpha2): void |
| 36 | { |
| 37 | if (1 !== preg_match('/^[a-zA-Z]{2}$/', $alpha2)) { |
| 38 | throw new DomainException(sprintf('Not a valid alpha2 key: %s', $alpha2)); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Assert that input looks like an alpha3 key. |
| 44 | * |
| 45 | * @throws \Automattic\WooCommerce\Vendor\League\ISO3166\Exception\DomainException if input does not look like an alpha3 key |
| 46 | */ |
| 47 | public static function guardAgainstInvalidAlpha3(string $alpha3): void |
| 48 | { |
| 49 | if (1 !== preg_match('/^[a-zA-Z]{3}$/', $alpha3)) { |
| 50 | throw new DomainException(sprintf('Not a valid alpha3 key: %s', $alpha3)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Assert that input looks like a numeric key. |
| 56 | * |
| 57 | * @throws \Automattic\WooCommerce\Vendor\League\ISO3166\Exception\DomainException if input does not look like a numeric key |
| 58 | */ |
| 59 | public static function guardAgainstInvalidNumeric(string $numeric): void |
| 60 | { |
| 61 | if (1 !== preg_match('/^\d{3}$/', $numeric)) { |
| 62 | throw new DomainException(sprintf('Not a valid numeric key: %s', $numeric)); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 |