Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
AllowedCountries Class Reference

Public Member Functions

 __construct (ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager)
 
 getAllowedCountries ( $scope=ScopeInterface::SCOPE_WEBSITE, $scopeCode=null)
 
 makeCountriesUnique (array $allowedCountries)
 
 getCountriesFromConfig ($scope, $scopeCode)
 

Data Fields

const ALLOWED_COUNTRIES_PATH = 'general/country/allow'
 

Detailed Description

Provider of allowed countries based on configuration settings

@api

Since
100.1.2

Definition at line 19 of file AllowedCountries.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ScopeConfigInterface  $scopeConfig,
StoreManagerInterface  $storeManager 
)
Parameters
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Store\Model\StoreManagerInterface$storeManager
Returns
void

Definition at line 38 of file AllowedCountries.php.

41  {
42  $this->scopeConfig = $scopeConfig;
43  $this->storeManager = $storeManager;
44  }

Member Function Documentation

◆ getAllowedCountries()

getAllowedCountries (   $scope = ScopeInterface::SCOPE_WEBSITE,
  $scopeCode = null 
)

Retrieve all allowed countries for scope or scopes

Parameters
string$scope
string | null$scopeCode
Returns
array
Since
100.1.2

Definition at line 54 of file AllowedCountries.php.

57  {
58  if ($scopeCode === null) {
59  $scopeCode = $this->getDefaultScopeCode($scope);
60  }
61 
62  switch ($scope) {
63  case ScopeInterface::SCOPE_WEBSITES:
64  case ScopeInterface::SCOPE_STORES:
65  $allowedCountries = [];
66  foreach ($scopeCode as $singleFilter) {
67  $allowedCountries = array_merge(
68  $allowedCountries,
69  $this->getCountriesFromConfig($this->getSingleScope($scope), $singleFilter)
70  );
71  }
72  break;
73  default:
74  $allowedCountries = $this->getCountriesFromConfig($scope, $scopeCode);
75  }
76 
77  return $this->makeCountriesUnique($allowedCountries);
78  }
makeCountriesUnique(array $allowedCountries)

◆ getCountriesFromConfig()

getCountriesFromConfig (   $scope,
  $scopeCode 
)

Takes countries from Countries Config data

Parameters
string$scope
int$scopeCode
Returns
array
Since
100.1.2

Definition at line 125 of file AllowedCountries.php.

126  {
127  return explode(
128  ',',
129  (string) $this->scopeConfig->getValue(
130  self::ALLOWED_COUNTRIES_PATH,
131  $scope,
132  $scopeCode
133  )
134  );
135  }

◆ makeCountriesUnique()

makeCountriesUnique ( array  $allowedCountries)

Return Unique Countries by merging them by keys

Parameters
array$allowedCountries
Returns
array
Since
100.1.2

Definition at line 112 of file AllowedCountries.php.

113  {
114  return array_combine($allowedCountries, $allowedCountries);
115  }

Field Documentation

◆ ALLOWED_COUNTRIES_PATH

const ALLOWED_COUNTRIES_PATH = 'general/country/allow'

Definition at line 21 of file AllowedCountries.php.


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