SubdivisionCode.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Rules;
  8. use Respect\Validation\Helpers\CountryInfo;
  9. use function array_keys;
  10. /**
  11. * Validates country subdivision codes according to ISO 3166-2.
  12. *
  13. * @see http://en.wikipedia.org/wiki/ISO_3166-2
  14. * @see http://www.geonames.org/countries/
  15. *
  16. * @author Henrique Moody <henriquemoody@gmail.com>
  17. * @author Mazen Touati <mazen_touati@hotmail.com>
  18. */
  19. final class SubdivisionCode extends AbstractSearcher
  20. {
  21. /**
  22. * @var string
  23. */
  24. private $countryName;
  25. /**
  26. * @var string[]
  27. */
  28. private $countryInfo;
  29. public function __construct(string $countryCode)
  30. {
  31. $countryInfo = new CountryInfo($countryCode);
  32. $this->countryName = $countryInfo->getCountry();
  33. $this->countryInfo = array_keys($countryInfo->getSubdivisions());
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. protected function getDataSource($input = null): array
  39. {
  40. return $this->countryInfo;
  41. }
  42. }