123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?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);
- }
- }
|