Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CountryInformationAcquirer.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
19 
24 
28  protected $directoryHelper;
29 
33  protected $scopeConfig;
34 
38  protected $storeManager;
39 
47  public function __construct(
48  \Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory,
49  \Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory,
50  \Magento\Directory\Helper\Data $directoryHelper,
51  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
53  ) {
54  $this->countryInformationFactory = $countryInformationFactory;
55  $this->regionInformationFactory = $regionInformationFactory;
56  $this->directoryHelper = $directoryHelper;
57  $this->scopeConfig = $scopeConfig;
58  $this->storeManager = $storeManager;
59  }
60 
64  public function getCountriesInfo()
65  {
66  $countriesInfo = [];
67 
69  $store = $this->storeManager->getStore();
70 
71  $storeLocale = $this->scopeConfig->getValue(
72  'general/locale/code',
73  \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
74  $store->getCode()
75  );
76 
77  $countries = $this->directoryHelper->getCountryCollection($store);
78  $regions = $this->directoryHelper->getRegionData();
79  foreach ($countries as $data) {
80  $countryInfo = $this->setCountryInfo($data, $regions, $storeLocale);
81  $countriesInfo[] = $countryInfo;
82  }
83 
84  return $countriesInfo;
85  }
86 
90  public function getCountryInfo($countryId)
91  {
92  $store = $this->storeManager->getStore();
93  $storeLocale = $this->scopeConfig->getValue(
94  'general/locale/code',
95  \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
96  $store->getCode()
97  );
98 
99  $countriesCollection = $this->directoryHelper->getCountryCollection($store)->load();
100  $regions = $this->directoryHelper->getRegionData();
101  $country = $countriesCollection->getItemById($countryId);
102 
103  if (!$country) {
104  throw new NoSuchEntityException(
105  __(
106  "The country isn't available."
107  )
108  );
109  }
110  $countryInfo = $this->setCountryInfo($country, $regions, $storeLocale);
111 
112  return $countryInfo;
113  }
114 
123  protected function setCountryInfo($country, $regions, $storeLocale)
124  {
125  $countryId = $country->getCountryId();
126  $countryInfo = $this->countryInformationFactory->create();
127  $countryInfo->setId($countryId);
128  $countryInfo->setTwoLetterAbbreviation($country->getData('iso2_code'));
129  $countryInfo->setThreeLetterAbbreviation($country->getData('iso3_code'));
130  $countryInfo->setFullNameLocale($country->getName($storeLocale));
131  $countryInfo->setFullNameEnglish($country->getName('en_US'));
132 
133  if (array_key_exists($countryId, $regions)) {
134  $regionsInfo = [];
135  foreach ($regions[$countryId] as $id => $regionData) {
136  $regionInfo = $this->regionInformationFactory->create();
137  $regionInfo->setId($id);
138  $regionInfo->setCode($regionData['code']);
139  $regionInfo->setName($regionData['name']);
140  $regionsInfo[] = $regionInfo;
141  }
142  $countryInfo->setAvailableRegions($regionsInfo);
143  }
144 
145  return $countryInfo;
146  }
147 }
__construct(\Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory, \Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager)
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13