Data.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Helper;
  7. use Magento\Directory\Model\Currency;
  8. use Magento\Directory\Model\CurrencyFactory;
  9. use Magento\Directory\Model\ResourceModel\Country\Collection;
  10. use Magento\Directory\Model\ResourceModel\Region\CollectionFactory;
  11. use Magento\Framework\App\Cache\Type\Config;
  12. use Magento\Framework\App\Helper\Context;
  13. use Magento\Framework\Json\Helper\Data as JsonData;
  14. use Magento\Store\Model\ScopeInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. /**
  17. * Directory data helper
  18. *
  19. * @api
  20. * @since 100.0.2
  21. */
  22. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  23. {
  24. /**
  25. * Config value that lists ISO2 country codes which have optional Zip/Postal pre-configured
  26. */
  27. const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH = 'general/country/optional_zip_countries';
  28. /*
  29. * Path to config value, which lists countries, for which state is required.
  30. */
  31. const XML_PATH_STATES_REQUIRED = 'general/region/state_required';
  32. /*
  33. * Path to config value, which detects whether or not display the state for the country, if it is not required
  34. */
  35. const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all';
  36. /**#@+
  37. * Path to config value, which is default country
  38. */
  39. const XML_PATH_DEFAULT_COUNTRY = 'general/country/default';
  40. const XML_PATH_DEFAULT_LOCALE = 'general/locale/code';
  41. const XML_PATH_DEFAULT_TIMEZONE = 'general/locale/timezone';
  42. /**#@-*/
  43. /**
  44. * Path to config value that contains codes of the most used countries.
  45. * Such countries can be shown on the top of the country list.
  46. */
  47. const XML_PATH_TOP_COUNTRIES = 'general/country/destinations';
  48. /**
  49. * Path to config value that contains weight unit
  50. */
  51. const XML_PATH_WEIGHT_UNIT = 'general/locale/weight_unit';
  52. /**
  53. * Country collection
  54. *
  55. * @var Collection
  56. */
  57. protected $_countryCollection;
  58. /**
  59. * Region collection
  60. *
  61. * @var \Magento\Directory\Model\ResourceModel\Region\Collection
  62. */
  63. protected $_regionCollection;
  64. /**
  65. * Json representation of regions data
  66. *
  67. * @var string
  68. */
  69. protected $_regionJson;
  70. /**
  71. * Currency cache
  72. *
  73. * @var array
  74. */
  75. protected $_currencyCache = [];
  76. /**
  77. * ISO2 country codes which have optional Zip/Postal pre-configured
  78. *
  79. * @var array
  80. */
  81. protected $_optZipCountries = null;
  82. /**
  83. * @var Config
  84. */
  85. protected $_configCacheType;
  86. /**
  87. * @var CollectionFactory
  88. */
  89. protected $_regCollectionFactory;
  90. /**
  91. * @var JsonData
  92. */
  93. protected $jsonHelper;
  94. /**
  95. * @var StoreManagerInterface
  96. */
  97. protected $_storeManager;
  98. /**
  99. * @var CurrencyFactory
  100. */
  101. protected $_currencyFactory;
  102. /**
  103. * Data constructor.
  104. *
  105. * @param Context $context
  106. * @param Config $configCacheType
  107. * @param Collection $countryCollection
  108. * @param CollectionFactory $regCollectionFactory
  109. * @param JsonData $jsonHelper
  110. * @param StoreManagerInterface $storeManager
  111. * @param CurrencyFactory $currencyFactory
  112. */
  113. public function __construct(
  114. Context $context,
  115. Config $configCacheType,
  116. Collection $countryCollection,
  117. CollectionFactory $regCollectionFactory,
  118. JsonData $jsonHelper,
  119. StoreManagerInterface $storeManager,
  120. CurrencyFactory $currencyFactory
  121. ) {
  122. parent::__construct($context);
  123. $this->_configCacheType = $configCacheType;
  124. $this->_countryCollection = $countryCollection;
  125. $this->_regCollectionFactory = $regCollectionFactory;
  126. $this->jsonHelper = $jsonHelper;
  127. $this->_storeManager = $storeManager;
  128. $this->_currencyFactory = $currencyFactory;
  129. }
  130. /**
  131. * Retrieve region collection
  132. *
  133. * @return \Magento\Directory\Model\ResourceModel\Region\Collection
  134. */
  135. public function getRegionCollection()
  136. {
  137. if (!$this->_regionCollection) {
  138. $this->_regionCollection = $this->_regCollectionFactory->create();
  139. $this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load();
  140. }
  141. return $this->_regionCollection;
  142. }
  143. /**
  144. * Retrieve country collection
  145. *
  146. * @param null|int|string|\Magento\Store\Model\Store $store
  147. * @return Collection
  148. */
  149. public function getCountryCollection($store = null)
  150. {
  151. if (!$this->_countryCollection->isLoaded()) {
  152. $this->_countryCollection->loadByStore($store);
  153. }
  154. return $this->_countryCollection;
  155. }
  156. /**
  157. * Retrieve regions data json
  158. *
  159. * @return string
  160. * @throws \Magento\Framework\Exception\NoSuchEntityException
  161. */
  162. public function getRegionJson()
  163. {
  164. \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
  165. if (!$this->_regionJson) {
  166. $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId();
  167. $json = $this->_configCacheType->load($cacheKey);
  168. if (empty($json)) {
  169. $regions = $this->getRegionData();
  170. $json = $this->jsonHelper->jsonEncode($regions);
  171. if ($json === false) {
  172. $json = 'false';
  173. }
  174. $this->_configCacheType->save($json, $cacheKey);
  175. }
  176. $this->_regionJson = $json;
  177. }
  178. \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
  179. return $this->_regionJson;
  180. }
  181. /**
  182. * Convert currency
  183. *
  184. * @param float $amount
  185. * @param string $from
  186. * @param string $to
  187. *
  188. * @return float
  189. * @SuppressWarnings(PHPMD.ShortVariable)
  190. * @throws \Magento\Framework\Exception\NoSuchEntityException
  191. */
  192. public function currencyConvert($amount, $from, $to = null)
  193. {
  194. if (empty($this->_currencyCache[$from])) {
  195. $this->_currencyCache[$from] = $this->_currencyFactory->create()->load($from);
  196. }
  197. if ($to === null) {
  198. $to = $this->_storeManager->getStore()->getCurrentCurrencyCode();
  199. }
  200. $converted = $this->_currencyCache[$from]->convert($amount, $to);
  201. return $converted;
  202. }
  203. /**
  204. * Return ISO2 country codes, which have optional Zip/Postal pre-configured
  205. *
  206. * @param bool $asJson
  207. * @return array|string
  208. */
  209. public function getCountriesWithOptionalZip($asJson = false)
  210. {
  211. if (null === $this->_optZipCountries) {
  212. $value = trim(
  213. $this->scopeConfig->getValue(
  214. self::OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH,
  215. ScopeInterface::SCOPE_STORE
  216. )
  217. );
  218. $this->_optZipCountries = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
  219. }
  220. if ($asJson) {
  221. return $this->jsonHelper->jsonEncode($this->_optZipCountries);
  222. }
  223. return $this->_optZipCountries;
  224. }
  225. /**
  226. * Check whether zip code is optional for specified country code
  227. *
  228. * @param string $countryCode
  229. * @return boolean
  230. */
  231. public function isZipCodeOptional($countryCode)
  232. {
  233. $this->getCountriesWithOptionalZip();
  234. return in_array($countryCode, $this->_optZipCountries);
  235. }
  236. /**
  237. * Returns the list of countries, for which region is required
  238. *
  239. * @param boolean $asJson
  240. * @return array|string
  241. */
  242. public function getCountriesWithStatesRequired($asJson = false)
  243. {
  244. $value = trim(
  245. $this->scopeConfig->getValue(
  246. self::XML_PATH_STATES_REQUIRED,
  247. ScopeInterface::SCOPE_STORE
  248. )
  249. );
  250. $countryList = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
  251. if ($asJson) {
  252. return $this->jsonHelper->jsonEncode($countryList);
  253. }
  254. return $countryList;
  255. }
  256. /**
  257. * Return, whether non-required state should be shown
  258. *
  259. * @return bool
  260. */
  261. public function isShowNonRequiredState()
  262. {
  263. return $this->scopeConfig->isSetFlag(
  264. self::XML_PATH_DISPLAY_ALL_STATES,
  265. ScopeInterface::SCOPE_STORE
  266. );
  267. }
  268. /**
  269. * Returns flag, which indicates whether region is required for specified country
  270. *
  271. * @param string $countryId
  272. * @return bool
  273. */
  274. public function isRegionRequired($countryId)
  275. {
  276. $countyList = $this->getCountriesWithStatesRequired();
  277. if (!is_array($countyList)) {
  278. return false;
  279. }
  280. return in_array($countryId, $countyList);
  281. }
  282. /**
  283. * Retrieve application base currency code
  284. *
  285. * @return string
  286. */
  287. public function getBaseCurrencyCode()
  288. {
  289. return $this->scopeConfig->getValue(
  290. Currency::XML_PATH_CURRENCY_BASE,
  291. 'default'
  292. );
  293. }
  294. /**
  295. * Return default country code
  296. *
  297. * @param \Magento\Store\Model\Store|string|int $store
  298. * @return string
  299. */
  300. public function getDefaultCountry($store = null)
  301. {
  302. return $this->scopeConfig->getValue(
  303. self::XML_PATH_DEFAULT_COUNTRY,
  304. ScopeInterface::SCOPE_STORE,
  305. $store
  306. );
  307. }
  308. /**
  309. * Retrieve regions data
  310. *
  311. * @return array
  312. */
  313. public function getRegionData()
  314. {
  315. $countryIds = [];
  316. foreach ($this->getCountryCollection() as $country) {
  317. $countryIds[] = $country->getCountryId();
  318. }
  319. $collection = $this->_regCollectionFactory->create();
  320. $collection->addCountryFilter($countryIds)->load();
  321. $regions = [
  322. 'config' => [
  323. 'show_all_regions' => $this->isShowNonRequiredState(),
  324. 'regions_required' => $this->getCountriesWithStatesRequired(),
  325. ],
  326. ];
  327. foreach ($collection as $region) {
  328. /** @var $region \Magento\Directory\Model\Region */
  329. if (!$region->getRegionId()) {
  330. continue;
  331. }
  332. $regions[$region->getCountryId()][$region->getRegionId()] = [
  333. 'code' => $region->getCode(),
  334. 'name' => (string)__($region->getName()),
  335. ];
  336. }
  337. return $regions;
  338. }
  339. /**
  340. * Retrieve list of codes of the most used countries
  341. *
  342. * @return array
  343. */
  344. public function getTopCountryCodes()
  345. {
  346. $configValue = (string)$this->scopeConfig->getValue(
  347. self::XML_PATH_TOP_COUNTRIES,
  348. ScopeInterface::SCOPE_STORE
  349. );
  350. return !empty($configValue) ? explode(',', $configValue) : [];
  351. }
  352. /**
  353. * Retrieve weight unit
  354. *
  355. * @return string
  356. */
  357. public function getWeightUnit()
  358. {
  359. return $this->scopeConfig->getValue(self::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE);
  360. }
  361. }