Allregion.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model\Config\Source;
  7. /**
  8. * Options provider for regions list
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Allregion implements \Magento\Framework\Option\ArrayInterface
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $_countries;
  19. /**
  20. * @var array
  21. */
  22. protected $_options;
  23. /**
  24. * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
  25. */
  26. protected $_countryCollectionFactory;
  27. /**
  28. * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
  29. */
  30. protected $_regionCollectionFactory;
  31. /**
  32. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
  33. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  34. */
  35. public function __construct(
  36. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
  37. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  38. ) {
  39. $this->_countryCollectionFactory = $countryCollectionFactory;
  40. $this->_regionCollectionFactory = $regionCollectionFactory;
  41. }
  42. /**
  43. * @param bool $isMultiselect
  44. * @return array
  45. */
  46. public function toOptionArray($isMultiselect = false)
  47. {
  48. if (!$this->_options) {
  49. $countriesArray = $this->_countryCollectionFactory->create()->load()->toOptionArray(false);
  50. $this->_countries = [];
  51. foreach ($countriesArray as $a) {
  52. $this->_countries[$a['value']] = $a['label'];
  53. }
  54. $countryRegions = [];
  55. $regionsCollection = $this->_regionCollectionFactory->create()->load();
  56. foreach ($regionsCollection as $region) {
  57. $countryRegions[$region->getCountryId()][$region->getId()] = $region->getDefaultName();
  58. }
  59. uksort($countryRegions, [$this, 'sortRegionCountries']);
  60. $this->_options = [];
  61. foreach ($countryRegions as $countryId => $regions) {
  62. $regionOptions = [];
  63. foreach ($regions as $regionId => $regionName) {
  64. $regionOptions[] = ['label' => $regionName, 'value' => $regionId];
  65. }
  66. $this->_options[] = ['label' => $this->_countries[$countryId], 'value' => $regionOptions];
  67. }
  68. }
  69. $options = $this->_options;
  70. if (!$isMultiselect) {
  71. array_unshift($options, ['value' => '', 'label' => '']);
  72. }
  73. return $options;
  74. }
  75. /**
  76. * @param string $a
  77. * @param string $b
  78. * @return int
  79. */
  80. public function sortRegionCountries($a, $b)
  81. {
  82. return strcmp($this->_countries[$a], $this->_countries[$b]);
  83. }
  84. }