Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DirectoryDataProcessor.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Directory\Helper\Data as DirectoryHelper;
12 
20 {
24  private $countryOptions;
25 
29  private $regionOptions;
30 
34  private $regionCollectionFactory;
35 
39  private $countryCollectionFactory;
40 
44  private $storeManager;
45 
49  private $directoryHelper;
50 
59  public function __construct(
60  \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection,
61  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection,
62  StoreResolverInterface $storeResolver,
63  DirectoryHelper $directoryHelper,
64  StoreManagerInterface $storeManager = null
65  ) {
66  $this->countryCollectionFactory = $countryCollection;
67  $this->regionCollectionFactory = $regionCollection;
68  $this->directoryHelper = $directoryHelper;
69  $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
70  }
71 
78  public function process($jsLayout)
79  {
80  if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
81  $jsLayout['components']['checkoutProvider']['dictionaries'] = [
82  'country_id' => $this->getCountryOptions(),
83  'region_id' => $this->getRegionOptions(),
84  ];
85  }
86 
87  return $jsLayout;
88  }
89 
95  private function getCountryOptions()
96  {
97  if (!isset($this->countryOptions)) {
98  $this->countryOptions = $this->countryCollectionFactory->create()->loadByStore(
99  $this->storeManager->getStore()->getId()
100  )->toOptionArray();
101  $this->countryOptions = $this->orderCountryOptions($this->countryOptions);
102  }
103 
104  return $this->countryOptions;
105  }
106 
112  private function getRegionOptions()
113  {
114  if (!isset($this->regionOptions)) {
115  $this->regionOptions = $this->regionCollectionFactory->create()->addAllowedCountriesFilter(
116  $this->storeManager->getStore()->getId()
117  )->toOptionArray();
118  }
119 
120  return $this->regionOptions;
121  }
122 
129  private function orderCountryOptions(array $countryOptions)
130  {
131  $topCountryCodes = $this->directoryHelper->getTopCountryCodes();
132  if (empty($topCountryCodes)) {
133  return $countryOptions;
134  }
135 
136  $headOptions = [];
137  $tailOptions = [[
138  'value' => 'delimiter',
139  'label' => '──────────',
140  'disabled' => true,
141  ]];
142  foreach ($countryOptions as $countryOption) {
143  if (empty($countryOption['value']) || in_array($countryOption['value'], $topCountryCodes)) {
144  $headOptions[] = $countryOption;
145  } else {
146  $tailOptions[] = $countryOption;
147  }
148  }
149  return array_merge($headOptions, $tailOptions);
150  }
151 }
__construct(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection, StoreResolverInterface $storeResolver, DirectoryHelper $directoryHelper, StoreManagerInterface $storeManager=null)