Country.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Customer country attribute source
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. /**
  15. * Class Country.
  16. * @package Magento\Customer\Model\ResourceModel\Address\Attribute\Source
  17. */
  18. class Country extends \Magento\Eav\Model\Entity\Attribute\Source\Table
  19. {
  20. /**
  21. * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
  22. */
  23. protected $_countriesFactory;
  24. /**
  25. * @var StoreManagerInterface
  26. */
  27. private $storeManager;
  28. /**
  29. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  30. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
  31. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
  32. */
  33. public function __construct(
  34. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
  35. \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
  36. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
  37. ) {
  38. $this->_countriesFactory = $countriesFactory;
  39. parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function getAllOptions($withEmpty = true, $defaultValues = false)
  45. {
  46. if (!$this->_options) {
  47. $this->_options = $this->_createCountriesCollection()->loadByStore(
  48. $this->getStoreManager()->getStore()->getId()
  49. )->toOptionArray();
  50. }
  51. return $this->_options;
  52. }
  53. /**
  54. * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  55. */
  56. protected function _createCountriesCollection()
  57. {
  58. return $this->_countriesFactory->create();
  59. }
  60. /**
  61. * Retrieve Store Manager
  62. * @deprecated 101.0.0
  63. * @return StoreManagerInterface
  64. */
  65. private function getStoreManager()
  66. {
  67. if (!$this->storeManager) {
  68. $this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
  69. }
  70. return $this->storeManager;
  71. }
  72. }