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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Directory\Helper;
use Magento\Directory\Model\Currency;
use Magento\Directory\Model\CurrencyFactory;
use Magento\Directory\Model\ResourceModel\Country\Collection;
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory;
use Magento\Framework\App\Cache\Type\Config;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Json\Helper\Data as JsonData;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
/**
* Directory data helper
*
* @api
* @since 100.0.2
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* Config value that lists ISO2 country codes which have optional Zip/Postal pre-configured
*/
const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH = 'general/country/optional_zip_countries';
/*
* Path to config value, which lists countries, for which state is required.
*/
const XML_PATH_STATES_REQUIRED = 'general/region/state_required';
/*
* Path to config value, which detects whether or not display the state for the country, if it is not required
*/
const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all';
/**#@+
* Path to config value, which is default country
*/
const XML_PATH_DEFAULT_COUNTRY = 'general/country/default';
const XML_PATH_DEFAULT_LOCALE = 'general/locale/code';
const XML_PATH_DEFAULT_TIMEZONE = 'general/locale/timezone';
/**#@-*/
/**
* Path to config value that contains codes of the most used countries.
* Such countries can be shown on the top of the country list.
*/
const XML_PATH_TOP_COUNTRIES = 'general/country/destinations';
/**
* Path to config value that contains weight unit
*/
const XML_PATH_WEIGHT_UNIT = 'general/locale/weight_unit';
/**
* Country collection
*
* @var Collection
*/
protected $_countryCollection;
/**
* Region collection
*
* @var \Magento\Directory\Model\ResourceModel\Region\Collection
*/
protected $_regionCollection;
/**
* Json representation of regions data
*
* @var string
*/
protected $_regionJson;
/**
* Currency cache
*
* @var array
*/
protected $_currencyCache = [];
/**
* ISO2 country codes which have optional Zip/Postal pre-configured
*
* @var array
*/
protected $_optZipCountries = null;
/**
* @var Config
*/
protected $_configCacheType;
/**
* @var CollectionFactory
*/
protected $_regCollectionFactory;
/**
* @var JsonData
*/
protected $jsonHelper;
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* @var CurrencyFactory
*/
protected $_currencyFactory;
/**
* Data constructor.
*
* @param Context $context
* @param Config $configCacheType
* @param Collection $countryCollection
* @param CollectionFactory $regCollectionFactory
* @param JsonData $jsonHelper
* @param StoreManagerInterface $storeManager
* @param CurrencyFactory $currencyFactory
*/
public function __construct(
Context $context,
Config $configCacheType,
Collection $countryCollection,
CollectionFactory $regCollectionFactory,
JsonData $jsonHelper,
StoreManagerInterface $storeManager,
CurrencyFactory $currencyFactory
) {
parent::__construct($context);
$this->_configCacheType = $configCacheType;
$this->_countryCollection = $countryCollection;
$this->_regCollectionFactory = $regCollectionFactory;
$this->jsonHelper = $jsonHelper;
$this->_storeManager = $storeManager;
$this->_currencyFactory = $currencyFactory;
}
/**
* Retrieve region collection
*
* @return \Magento\Directory\Model\ResourceModel\Region\Collection
*/
public function getRegionCollection()
{
if (!$this->_regionCollection) {
$this->_regionCollection = $this->_regCollectionFactory->create();
$this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load();
}
return $this->_regionCollection;
}
/**
* Retrieve country collection
*
* @param null|int|string|\Magento\Store\Model\Store $store
* @return Collection
*/
public function getCountryCollection($store = null)
{
if (!$this->_countryCollection->isLoaded()) {
$this->_countryCollection->loadByStore($store);
}
return $this->_countryCollection;
}
/**
* Retrieve regions data json
*
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getRegionJson()
{
\Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
if (!$this->_regionJson) {
$cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId();
$json = $this->_configCacheType->load($cacheKey);
if (empty($json)) {
$regions = $this->getRegionData();
$json = $this->jsonHelper->jsonEncode($regions);
if ($json === false) {
$json = 'false';
}
$this->_configCacheType->save($json, $cacheKey);
}
$this->_regionJson = $json;
}
\Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
return $this->_regionJson;
}
/**
* Convert currency
*
* @param float $amount
* @param string $from
* @param string $to
*
* @return float
* @SuppressWarnings(PHPMD.ShortVariable)
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function currencyConvert($amount, $from, $to = null)
{
if (empty($this->_currencyCache[$from])) {
$this->_currencyCache[$from] = $this->_currencyFactory->create()->load($from);
}
if ($to === null) {
$to = $this->_storeManager->getStore()->getCurrentCurrencyCode();
}
$converted = $this->_currencyCache[$from]->convert($amount, $to);
return $converted;
}
/**
* Return ISO2 country codes, which have optional Zip/Postal pre-configured
*
* @param bool $asJson
* @return array|string
*/
public function getCountriesWithOptionalZip($asJson = false)
{
if (null === $this->_optZipCountries) {
$value = trim(
$this->scopeConfig->getValue(
self::OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH,
ScopeInterface::SCOPE_STORE
)
);
$this->_optZipCountries = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
}
if ($asJson) {
return $this->jsonHelper->jsonEncode($this->_optZipCountries);
}
return $this->_optZipCountries;
}
/**
* Check whether zip code is optional for specified country code
*
* @param string $countryCode
* @return boolean
*/
public function isZipCodeOptional($countryCode)
{
$this->getCountriesWithOptionalZip();
return in_array($countryCode, $this->_optZipCountries);
}
/**
* Returns the list of countries, for which region is required
*
* @param boolean $asJson
* @return array|string
*/
public function getCountriesWithStatesRequired($asJson = false)
{
$value = trim(
$this->scopeConfig->getValue(
self::XML_PATH_STATES_REQUIRED,
ScopeInterface::SCOPE_STORE
)
);
$countryList = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
if ($asJson) {
return $this->jsonHelper->jsonEncode($countryList);
}
return $countryList;
}
/**
* Return, whether non-required state should be shown
*
* @return bool
*/
public function isShowNonRequiredState()
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_DISPLAY_ALL_STATES,
ScopeInterface::SCOPE_STORE
);
}
/**
* Returns flag, which indicates whether region is required for specified country
*
* @param string $countryId
* @return bool
*/
public function isRegionRequired($countryId)
{
$countyList = $this->getCountriesWithStatesRequired();
if (!is_array($countyList)) {
return false;
}
return in_array($countryId, $countyList);
}
/**
* Retrieve application base currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
return $this->scopeConfig->getValue(
Currency::XML_PATH_CURRENCY_BASE,
'default'
);
}
/**
* Return default country code
*
* @param \Magento\Store\Model\Store|string|int $store
* @return string
*/
public function getDefaultCountry($store = null)
{
return $this->scopeConfig->getValue(
self::XML_PATH_DEFAULT_COUNTRY,
ScopeInterface::SCOPE_STORE,
$store
);
}
/**
* Retrieve regions data
*
* @return array
*/
public function getRegionData()
{
$countryIds = [];
foreach ($this->getCountryCollection() as $country) {
$countryIds[] = $country->getCountryId();
}
$collection = $this->_regCollectionFactory->create();
$collection->addCountryFilter($countryIds)->load();
$regions = [
'config' => [
'show_all_regions' => $this->isShowNonRequiredState(),
'regions_required' => $this->getCountriesWithStatesRequired(),
],
];
foreach ($collection as $region) {
/** @var $region \Magento\Directory\Model\Region */
if (!$region->getRegionId()) {
continue;
}
$regions[$region->getCountryId()][$region->getRegionId()] = [
'code' => $region->getCode(),
'name' => (string)__($region->getName()),
];
}
return $regions;
}
/**
* Retrieve list of codes of the most used countries
*
* @return array
*/
public function getTopCountryCodes()
{
$configValue = (string)$this->scopeConfig->getValue(
self::XML_PATH_TOP_COUNTRIES,
ScopeInterface::SCOPE_STORE
);
return !empty($configValue) ? explode(',', $configValue) : [];
}
/**
* Retrieve weight unit
*
* @return string
*/
public function getWeightUnit()
{
return $this->scopeConfig->getValue(self::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE);
}
}