LocationDirectory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
  7. /**
  8. * Location directory.
  9. */
  10. class LocationDirectory
  11. {
  12. /**
  13. * @var array
  14. */
  15. protected $regions;
  16. /**
  17. * @var array
  18. */
  19. private $regionsByCode;
  20. /**
  21. * @var array
  22. */
  23. protected $iso2Countries;
  24. /**
  25. * @var array
  26. */
  27. protected $iso3Countries;
  28. /**
  29. * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
  30. */
  31. protected $_countryCollectionFactory;
  32. /**
  33. * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
  34. */
  35. protected $_regionCollectionFactory;
  36. /**
  37. * LocationDirectory constructor.
  38. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
  39. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  40. */
  41. public function __construct(
  42. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
  43. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  44. ) {
  45. $this->_countryCollectionFactory = $countryCollectionFactory;
  46. $this->_regionCollectionFactory = $regionCollectionFactory;
  47. }
  48. /**
  49. * Retrieve country id.
  50. *
  51. * @param string $countryCode
  52. * @return null|string
  53. */
  54. public function getCountryId($countryCode)
  55. {
  56. $this->loadCountries();
  57. $countryId = null;
  58. if (isset($this->iso2Countries[$countryCode])) {
  59. $countryId = $this->iso2Countries[$countryCode];
  60. } elseif (isset($this->iso3Countries[$countryCode])) {
  61. $countryId = $this->iso3Countries[$countryCode];
  62. }
  63. return $countryId;
  64. }
  65. /**
  66. * Load directory countries
  67. *
  68. * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
  69. */
  70. protected function loadCountries()
  71. {
  72. if ($this->iso2Countries !== null && $this->iso3Countries !== null) {
  73. return $this;
  74. }
  75. $this->iso2Countries = [];
  76. $this->iso3Countries = [];
  77. /** @var $collection \Magento\Directory\Model\ResourceModel\Country\Collection */
  78. $collection = $this->_countryCollectionFactory->create();
  79. foreach ($collection->getData() as $row) {
  80. $this->iso2Countries[$row['iso2_code']] = $row['country_id'];
  81. $this->iso3Countries[$row['iso3_code']] = $row['country_id'];
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Check if there is country id with provided country code.
  87. *
  88. * @param string $countryCode
  89. * @return bool
  90. */
  91. public function hasCountryId($countryCode)
  92. {
  93. $this->loadCountries();
  94. return isset($this->iso2Countries[$countryCode]) || isset($this->iso3Countries[$countryCode]);
  95. }
  96. /**
  97. * Check if there is region id with provided region code and country id.
  98. *
  99. * @param string $countryId
  100. * @param string $regionCode
  101. * @return bool
  102. */
  103. public function hasRegionId($countryId, $regionCode)
  104. {
  105. $this->loadRegions();
  106. return isset($this->regions[$countryId][$regionCode]);
  107. }
  108. /**
  109. * Load directory regions
  110. *
  111. * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
  112. */
  113. protected function loadRegions()
  114. {
  115. if ($this->regions !== null && $this->regionsByCode !== null) {
  116. return $this;
  117. }
  118. $this->regions = [];
  119. $this->regionsByCode = [];
  120. /** @var $collection \Magento\Directory\Model\ResourceModel\Region\Collection */
  121. $collection = $this->_regionCollectionFactory->create();
  122. foreach ($collection->getData() as $row) {
  123. $this->regions[$row['country_id']][$row['code']] = (int)$row['region_id'];
  124. if (empty($this->regionsByCode[$row['country_id']][$row['code']])) {
  125. $this->regionsByCode[$row['country_id']][$row['code']] = [];
  126. }
  127. $this->regionsByCode[$row['country_id']][$row['code']][] = (int)$row['region_id'];
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Retrieve region id.
  133. *
  134. * @param int $countryId
  135. * @param string $regionCode
  136. * @return string
  137. * @deprecated 100.3.1
  138. */
  139. public function getRegionId($countryId, $regionCode)
  140. {
  141. $this->loadRegions();
  142. return $this->regions[$countryId][$regionCode];
  143. }
  144. /**
  145. * Return region ids for country and region
  146. *
  147. * @param int $countryId
  148. * @param string $regionCode
  149. * @return array
  150. */
  151. public function getRegionIds($countryId, $regionCode)
  152. {
  153. $this->loadRegions();
  154. return $this->regionsByCode[$countryId][$regionCode];
  155. }
  156. }