Region.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\System\Config\Source\Tax;
  7. class Region implements \Magento\Framework\Option\ArrayInterface
  8. {
  9. /**
  10. * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
  11. */
  12. protected $_regionsFactory;
  13. /**
  14. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionsFactory
  15. */
  16. public function __construct(\Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionsFactory)
  17. {
  18. $this->_regionsFactory = $regionsFactory;
  19. }
  20. /**
  21. * Return list of country's regions as array
  22. *
  23. * @param bool $noEmpty
  24. * @param string|array|null $country
  25. * @return array
  26. */
  27. public function toOptionArray($noEmpty = false, $country = null)
  28. {
  29. /** @var $region \Magento\Directory\Model\ResourceModel\Region\Collection */
  30. $regionCollection = $this->_regionsFactory->create();
  31. $options = $regionCollection->addCountryFilter($country)->toOptionArray();
  32. if ($noEmpty) {
  33. unset($options[0]);
  34. } else {
  35. if ($options) {
  36. $options[0] = ['value' => '0', 'label' => '*'];
  37. } else {
  38. $options = [['value' => '0', 'label' => '*']];
  39. }
  40. }
  41. return $options;
  42. }
  43. }