CountryInfo.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Helpers;
  8. use Respect\Validation\Exceptions\ComponentException;
  9. use function file_exists;
  10. use function sprintf;
  11. final class CountryInfo
  12. {
  13. /**
  14. * @var mixed[]
  15. */
  16. private $data;
  17. /**
  18. * @var mixed[]
  19. */
  20. private static $runtimeCache = [];
  21. public function __construct(string $countryCode)
  22. {
  23. if (!isset(static::$runtimeCache[$countryCode])) {
  24. $filename = __DIR__ . '/../../data/iso_3166-2/' . $countryCode . '.php';
  25. if (!file_exists($filename)) {
  26. throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
  27. }
  28. static::$runtimeCache[$countryCode] = require $filename;
  29. }
  30. $this->data = static::$runtimeCache[$countryCode];
  31. }
  32. public function getCountry(): string
  33. {
  34. return $this->data['country'];
  35. }
  36. /**
  37. * @return string[]
  38. */
  39. public function getSubdivisions(): array
  40. {
  41. return $this->data['subdivisions'];
  42. }
  43. }