Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Data Class Reference
Inheritance diagram for Data:
AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regCollectionFactory, \Magento\Framework\Json\Helper\Data $jsonHelper, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Directory\Model\CurrencyFactory $currencyFactory)
 
 getRegionCollection ()
 
 getCountryCollection ($store=null)
 
 getRegionJson ()
 
 currencyConvert ($amount, $from, $to=null)
 
 getCountriesWithOptionalZip ($asJson=false)
 
 isZipCodeOptional ($countryCode)
 
 getCountriesWithStatesRequired ($asJson=false)
 
 isShowNonRequiredState ()
 
 isRegionRequired ($countryId)
 
 getBaseCurrencyCode ()
 
 getDefaultCountry ($store=null)
 
 getTopCountryCodes ()
 
 getWeightUnit ()
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH = 'general/country/optional_zip_countries'
 
const XML_PATH_STATES_REQUIRED = 'general/region/state_required'
 
const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all'
 
const XML_PATH_DEFAULT_COUNTRY = 'general/country/default'
 
const XML_PATH_DEFAULT_LOCALE = 'general/locale/code'
 
const XML_PATH_DEFAULT_TIMEZONE = 'general/locale/timezone'
 
const XML_PATH_TOP_COUNTRIES = 'general/country/destinations'
 
const XML_PATH_WEIGHT_UNIT = 'general/locale/weight_unit'
 

Protected Attributes

 $_countryCollection
 
 $_regionCollection
 
 $_regionJson
 
 $_currencyCache = []
 
 $_optZipCountries = null
 
 $_configCacheType
 
 $_regCollectionFactory
 
 $jsonHelper
 
 $_storeManager
 
 $_currencyFactory
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Detailed Description

Directory data helper

@api

Since
100.0.2

Definition at line 17 of file Data.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Helper\Context  $context,
\Magento\Framework\App\Cache\Type\Config  $configCacheType,
\Magento\Directory\Model\ResourceModel\Country\Collection  $countryCollection,
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory  $regCollectionFactory,
\Magento\Framework\Json\Helper\Data  $jsonHelper,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Directory\Model\CurrencyFactory  $currencyFactory 
)
Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Framework\App\Cache\Type\Config$configCacheType
\Magento\Directory\Model\ResourceModel\Country\Collection$countryCollection
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory$regCollectionFactory

Definition at line 122 of file Data.php.

130  {
131  parent::__construct($context);
132  $this->_configCacheType = $configCacheType;
133  $this->_countryCollection = $countryCollection;
134  $this->_regCollectionFactory = $regCollectionFactory;
135  $this->jsonHelper = $jsonHelper;
136  $this->_storeManager = $storeManager;
137  $this->_currencyFactory = $currencyFactory;
138  }
$storeManager

Member Function Documentation

◆ currencyConvert()

currencyConvert (   $amount,
  $from,
  $to = null 
)

Convert currency

Parameters
float$amount
string$from
string$to
Returns
float @SuppressWarnings(PHPMD.ShortVariable)

Definition at line 203 of file Data.php.

204  {
205  if (empty($this->_currencyCache[$from])) {
206  $this->_currencyCache[$from] = $this->_currencyFactory->create()->load($from);
207  }
208  if ($to === null) {
209  $to = $this->_storeManager->getStore()->getCurrentCurrencyCode();
210  }
211  $converted = $this->_currencyCache[$from]->convert($amount, $to);
212  return $converted;
213  }
$amount
Definition: order.php:14

◆ getBaseCurrencyCode()

getBaseCurrencyCode ( )

Retrieve application base currency code

Returns
string

Definition at line 304 of file Data.php.

305  {
306  return $this->scopeConfig->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default');
307  }

◆ getCountriesWithOptionalZip()

getCountriesWithOptionalZip (   $asJson = false)

Return ISO2 country codes, which have optional Zip/Postal pre-configured

Parameters
bool$asJson
Returns
array|string

Definition at line 221 of file Data.php.

222  {
223  if (null === $this->_optZipCountries) {
224  $value = trim(
225  $this->scopeConfig->getValue(
226  self::OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH,
228  )
229  );
230  $this->_optZipCountries = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
231  }
232  if ($asJson) {
233  return $this->jsonHelper->jsonEncode($this->_optZipCountries);
234  }
236  }
$value
Definition: gender.phtml:16

◆ getCountriesWithStatesRequired()

getCountriesWithStatesRequired (   $asJson = false)

Returns the list of countries, for which region is required

Parameters
boolean$asJson
Returns
array

Definition at line 256 of file Data.php.

257  {
258  $value = trim(
259  $this->scopeConfig->getValue(
260  self::XML_PATH_STATES_REQUIRED,
262  )
263  );
264  $countryList = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
265  if ($asJson) {
266  return $this->jsonHelper->jsonEncode($countryList);
267  }
268  return $countryList;
269  }
$value
Definition: gender.phtml:16

◆ getCountryCollection()

getCountryCollection (   $store = null)

Retrieve country collection

Parameters
null | int | string | \Magento\Store\Model\Store$store
Returns
\Magento\Directory\Model\ResourceModel\Country\Collection

Definition at line 160 of file Data.php.

161  {
162  if (!$this->_countryCollection->isLoaded()) {
163  $this->_countryCollection->loadByStore($store);
164  }
166  }

◆ getDefaultCountry()

getDefaultCountry (   $store = null)

Return default country code

Parameters
\Magento\Store\Model\Store | string | int$store
Returns
string

Definition at line 315 of file Data.php.

316  {
317  return $this->scopeConfig->getValue(
318  self::XML_PATH_DEFAULT_COUNTRY,
320  $store
321  );
322  }

◆ getRegionCollection()

getRegionCollection ( )

Retrieve region collection

Returns
\Magento\Directory\Model\ResourceModel\Region\Collection

Definition at line 145 of file Data.php.

146  {
147  if (!$this->_regionCollection) {
148  $this->_regionCollection = $this->_regCollectionFactory->create();
149  $this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load();
150  }
152  }

◆ getRegionJson()

getRegionJson ( )

Retrieve regions data json

Returns
string

Definition at line 173 of file Data.php.

174  {
175  \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
176  if (!$this->_regionJson) {
177  $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId();
178  $json = $this->_configCacheType->load($cacheKey);
179  if (empty($json)) {
180  $regions = $this->getRegionData();
181  $json = $this->jsonHelper->jsonEncode($regions);
182  if ($json === false) {
183  $json = 'false';
184  }
185  $this->_configCacheType->save($json, $cacheKey);
186  }
187  $this->_regionJson = $json;
188  }
189 
190  \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
191  return $this->_regionJson;
192  }

◆ getTopCountryCodes()

getTopCountryCodes ( )

Retrieve list of codes of the most used countries

Returns
array

Definition at line 361 of file Data.php.

362  {
363  $configValue = (string)$this->scopeConfig->getValue(
364  self::XML_PATH_TOP_COUNTRIES,
366  );
367  return !empty($configValue) ? explode(',', $configValue) : [];
368  }

◆ getWeightUnit()

getWeightUnit ( )

Retrieve weight unit

Returns
string

Definition at line 375 of file Data.php.

376  {
377  return $this->scopeConfig->getValue(self::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE);
378  }

◆ isRegionRequired()

isRegionRequired (   $countryId)

Returns flag, which indicates whether region is required for specified country

Parameters
string$countryId
Returns
bool

Definition at line 290 of file Data.php.

291  {
292  $countyList = $this->getCountriesWithStatesRequired();
293  if (!is_array($countyList)) {
294  return false;
295  }
296  return in_array($countryId, $countyList);
297  }
getCountriesWithStatesRequired($asJson=false)
Definition: Data.php:256

◆ isShowNonRequiredState()

isShowNonRequiredState ( )

Return, whether non-required state should be shown

Returns
bool

Definition at line 276 of file Data.php.

277  {
278  return (bool)$this->scopeConfig->getValue(
279  self::XML_PATH_DISPLAY_ALL_STATES,
281  );
282  }

◆ isZipCodeOptional()

isZipCodeOptional (   $countryCode)

Check whether zip code is optional for specified country code

Parameters
string$countryCode
Returns
boolean

Definition at line 244 of file Data.php.

245  {
247  return in_array($countryCode, $this->_optZipCountries);
248  }
getCountriesWithOptionalZip($asJson=false)
Definition: Data.php:221

Field Documentation

◆ $_configCacheType

$_configCacheType
protected

Definition at line 91 of file Data.php.

◆ $_countryCollection

$_countryCollection
protected

Definition at line 58 of file Data.php.

◆ $_currencyCache

$_currencyCache = []
protected

Definition at line 79 of file Data.php.

◆ $_currencyFactory

$_currencyFactory
protected

Definition at line 111 of file Data.php.

◆ $_optZipCountries

$_optZipCountries = null
protected

Definition at line 86 of file Data.php.

◆ $_regCollectionFactory

$_regCollectionFactory
protected

Definition at line 96 of file Data.php.

◆ $_regionCollection

$_regionCollection
protected

Definition at line 65 of file Data.php.

◆ $_regionJson

$_regionJson
protected

Definition at line 72 of file Data.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 106 of file Data.php.

◆ $jsonHelper

$jsonHelper
protected

Definition at line 101 of file Data.php.

◆ OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH

const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH = 'general/country/optional_zip_countries'

Config value that lists ISO2 country codes which have optional Zip/Postal pre-configured

Definition at line 22 of file Data.php.

◆ XML_PATH_DEFAULT_COUNTRY

const XML_PATH_DEFAULT_COUNTRY = 'general/country/default'

#+ Path to config value, which is default country

Definition at line 37 of file Data.php.

◆ XML_PATH_DEFAULT_LOCALE

const XML_PATH_DEFAULT_LOCALE = 'general/locale/code'

Definition at line 38 of file Data.php.

◆ XML_PATH_DEFAULT_TIMEZONE

const XML_PATH_DEFAULT_TIMEZONE = 'general/locale/timezone'

Definition at line 39 of file Data.php.

◆ XML_PATH_DISPLAY_ALL_STATES

const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all'

Definition at line 32 of file Data.php.

◆ XML_PATH_STATES_REQUIRED

const XML_PATH_STATES_REQUIRED = 'general/region/state_required'

Definition at line 27 of file Data.php.

◆ XML_PATH_TOP_COUNTRIES

const XML_PATH_TOP_COUNTRIES = 'general/country/destinations'

#- Path to config value that contains codes of the most used countries. Such countries can be shown on the top of the country list.

Definition at line 46 of file Data.php.

◆ XML_PATH_WEIGHT_UNIT

const XML_PATH_WEIGHT_UNIT = 'general/locale/weight_unit'

Path to config value that contains weight unit

Definition at line 51 of file Data.php.


The documentation for this class was generated from the following file: