1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Directory\Model;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Currency information acquirer class
*/
class CountryInformationAcquirer implements \Magento\Directory\Api\CountryInformationAcquirerInterface
{
/**
* @var \Magento\Directory\Model\Data\CountryInformationFactory
*/
protected $countryInformationFactory;
/**
* @var \Magento\Directory\Model\Data\RegionInformationFactory
*/
protected $regionInformationFactory;
/**
* @var \Magento\Directory\Helper\Data
*/
protected $directoryHelper;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @param \Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory
* @param \Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory
* @param \Magento\Directory\Helper\Data $directoryHelper
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __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
) {
$this->countryInformationFactory = $countryInformationFactory;
$this->regionInformationFactory = $regionInformationFactory;
$this->directoryHelper = $directoryHelper;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}
/**
* {@inheritdoc}
*/
public function getCountriesInfo()
{
$countriesInfo = [];
/** @var \Magento\Store\Model\Store $store */
$store = $this->storeManager->getStore();
$storeLocale = $this->scopeConfig->getValue(
'general/locale/code',
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
$countries = $this->directoryHelper->getCountryCollection($store);
$regions = $this->directoryHelper->getRegionData();
foreach ($countries as $data) {
$countryInfo = $this->setCountryInfo($data, $regions, $storeLocale);
$countriesInfo[] = $countryInfo;
}
return $countriesInfo;
}
/**
* {@inheritdoc}
*/
public function getCountryInfo($countryId)
{
$store = $this->storeManager->getStore();
$storeLocale = $this->scopeConfig->getValue(
'general/locale/code',
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
$countriesCollection = $this->directoryHelper->getCountryCollection($store)->load();
$regions = $this->directoryHelper->getRegionData();
$country = $countriesCollection->getItemById($countryId);
if (!$country) {
throw new NoSuchEntityException(
__(
"The country isn't available."
)
);
}
$countryInfo = $this->setCountryInfo($country, $regions, $storeLocale);
return $countryInfo;
}
/**
* Creates and initializes the information for \Magento\Directory\Model\Data\CountryInformation
*
* @param \Magento\Directory\Model\ResourceModel\Country $country
* @param array $regions
* @param string $storeLocale
* @return \Magento\Directory\Model\Data\CountryInformation
*/
protected function setCountryInfo($country, $regions, $storeLocale)
{
$countryId = $country->getCountryId();
$countryInfo = $this->countryInformationFactory->create();
$countryInfo->setId($countryId);
$countryInfo->setTwoLetterAbbreviation($country->getData('iso2_code'));
$countryInfo->setThreeLetterAbbreviation($country->getData('iso3_code'));
$countryInfo->setFullNameLocale($country->getName($storeLocale));
$countryInfo->setFullNameEnglish($country->getName('en_US'));
if (array_key_exists($countryId, $regions)) {
$regionsInfo = [];
foreach ($regions[$countryId] as $id => $regionData) {
$regionInfo = $this->regionInformationFactory->create();
$regionInfo->setId($id);
$regionInfo->setCode($regionData['code']);
$regionInfo->setName($regionData['name']);
$regionsInfo[] = $regionInfo;
}
$countryInfo->setAvailableRegions($regionsInfo);
}
return $countryInfo;
}
}