Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LocationDirectory.php
Go to the documentation of this file.
1 <?php
8 
10 {
14  protected $regions;
15 
19  protected $iso2Countries;
20 
24  protected $iso3Countries;
25 
30 
35 
41  public function __construct(
42  \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
43  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
44  ) {
45  $this->_countryCollectionFactory = $countryCollectionFactory;
46  $this->_regionCollectionFactory = $regionCollectionFactory;
47  }
48 
53  public function getCountryId($countryCode)
54  {
55  $this->loadCountries();
56  $countryId = null;
57  if (isset($this->iso2Countries[$countryCode])) {
58  $countryId = $this->iso2Countries[$countryCode];
59  } elseif (isset($this->iso3Countries[$countryCode])) {
60  $countryId = $this->iso3Countries[$countryCode];
61  }
62 
63  return $countryId;
64  }
65 
71  protected function loadCountries()
72  {
73  if ($this->iso2Countries !== null && $this->iso3Countries !== null) {
74  return $this;
75  }
76 
77  $this->iso2Countries = [];
78  $this->iso3Countries = [];
79 
81  $collection = $this->_countryCollectionFactory->create();
82  foreach ($collection->getData() as $row) {
83  $this->iso2Countries[$row['iso2_code']] = $row['country_id'];
84  $this->iso3Countries[$row['iso3_code']] = $row['country_id'];
85  }
86 
87  return $this;
88  }
89 
94  public function hasCountryId($countryCode)
95  {
96  $this->loadCountries();
97  return isset($this->iso2Countries[$countryCode]) || isset($this->iso3Countries[$countryCode]);
98  }
99 
105  public function hasRegionId($countryId, $regionCode)
106  {
107  $this->loadRegions();
108  return isset($this->regions[$countryId][$regionCode]);
109  }
110 
116  protected function loadRegions()
117  {
118  if ($this->regions !== null) {
119  return $this;
120  }
121 
122  $this->regions = [];
123 
125  $collection = $this->_regionCollectionFactory->create();
126  foreach ($collection->getData() as $row) {
127  $this->regions[$row['country_id']][$row['code']] = (int)$row['region_id'];
128  }
129 
130  return $this;
131  }
132 
138  public function getRegionId($countryId, $regionCode)
139  {
140  $this->loadRegions();
141  return $this->regions[$countryId][$regionCode];
142  }
143 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory)