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
ISO3166DataValidator.php
64 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 ISO3166DataValidator |
| 17 | { |
| 18 | /** |
| 19 | * @param array<array<string, mixed>> $data |
| 20 | * |
| 21 | * @return array<array<string, mixed>> |
| 22 | */ |
| 23 | public function validate(array $data): array |
| 24 | { |
| 25 | foreach ($data as $entry) { |
| 26 | $this->assertEntryHasRequiredKeys($entry); |
| 27 | } |
| 28 | |
| 29 | return $data; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param array<string, mixed> $entry |
| 34 | * |
| 35 | * @throws \Automattic\WooCommerce\Vendor\League\ISO3166\Exception\DomainException if given data entry does not have all the required keys |
| 36 | */ |
| 37 | private function assertEntryHasRequiredKeys(array $entry): void |
| 38 | { |
| 39 | if (!isset($entry[ISO3166::KEY_NAME])) { |
| 40 | throw new DomainException('Each data entry must have a name key.'); |
| 41 | } |
| 42 | |
| 43 | Guards::guardAgainstInvalidName($entry[ISO3166::KEY_NAME]); |
| 44 | |
| 45 | if (!isset($entry[ISO3166::KEY_ALPHA2])) { |
| 46 | throw new DomainException('Each data entry must have a alpha2 key.'); |
| 47 | } |
| 48 | |
| 49 | Guards::guardAgainstInvalidAlpha2($entry[ISO3166::KEY_ALPHA2]); |
| 50 | |
| 51 | if (!isset($entry[ISO3166::KEY_ALPHA3])) { |
| 52 | throw new DomainException('Each data entry must have a alpha3 key.'); |
| 53 | } |
| 54 | |
| 55 | Guards::guardAgainstInvalidAlpha3($entry[ISO3166::KEY_ALPHA3]); |
| 56 | |
| 57 | if (!isset($entry[ISO3166::KEY_NUMERIC])) { |
| 58 | throw new DomainException('Each data entry must have a numeric key.'); |
| 59 | } |
| 60 | |
| 61 | Guards::guardAgainstInvalidNumeric($entry[ISO3166::KEY_NUMERIC]); |
| 62 | } |
| 63 | } |
| 64 |