AllowedCountries.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Config\Source;
  7. use Magento\Directory\Model\ResourceModel\Country\Collection;
  8. use Magento\Framework\Data\OptionSourceInterface;
  9. use Vertex\Tax\Model\Config;
  10. /**
  11. * Provides a list of countries indexed by the Vertex region they're associated with
  12. */
  13. class AllowedCountries implements OptionSourceInterface
  14. {
  15. /** @var Config */
  16. private $config;
  17. /** @var Collection */
  18. private $countryCollection;
  19. /**
  20. * @param Config $config
  21. * @param Collection $countryCollection
  22. */
  23. public function __construct(Config $config, Collection $countryCollection)
  24. {
  25. $this->config = $config;
  26. $this->countryCollection = $countryCollection;
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. public function toOptionArray()
  32. {
  33. /**
  34. * @var array $return Format expected by toOptionArray
  35. * @var array $countries Indexed array of [label => Country Name, value => Country Code]
  36. * @var string[] $countryNames Mapped by 2-character ISO code
  37. * @var string[] $regionCountryMap Mapping of country codes (index) to their region name (value)
  38. * @var array $regionMap Array of pointers to entries on the {@see $return} variable, indexed by region name
  39. * @var string[] $regions List of regions
  40. */
  41. $return = [];
  42. $countries = $this->countryCollection->loadData()->toOptionArray();
  43. array_shift($countries);
  44. $countryNames = array_column($countries, 'label', 'value');
  45. asort($countryNames);
  46. $regionCountryMap = $this->getRegionMap();
  47. $regionMap = [];
  48. $regions = $this->getRegions();
  49. foreach ($regions as $region) {
  50. $index = count($return);
  51. $return[$index] = [
  52. 'label' => __($region),
  53. 'value' => [],
  54. ];
  55. $regionMap[$region] = &$return[$index];
  56. }
  57. foreach ($countryNames as $countryCode => $countryName) {
  58. $region = isset($regionCountryMap[$countryCode]) ? $regionCountryMap[$countryCode] : 'Others';
  59. $regionInReturn = &$regionMap[$region];
  60. $regionInReturn['value'][] = [
  61. 'label' => __($countryName),
  62. 'value' => $countryCode,
  63. ];
  64. }
  65. return $return;
  66. }
  67. /**
  68. * Retrieve a mapping of country codes to regions
  69. *
  70. * @return string[] Map of country codes (as index) to their region (as value)
  71. */
  72. private function getRegionMap()
  73. {
  74. $regions = $this->config->getListForAllowedCountrySort();
  75. $result = [];
  76. foreach ($regions as $region => $countries) {
  77. foreach ($countries as $country) {
  78. $result[$country] = $region;
  79. }
  80. }
  81. return $result;
  82. }
  83. /**
  84. * Retrieve a list of regions
  85. *
  86. * @return string[]
  87. */
  88. private function getRegions()
  89. {
  90. $regions = $this->config->getListForAllowedCountrySort();
  91. return array_keys($regions);
  92. }
  93. }