Country.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Adminhtml\System\Config;
  7. use Magento\Directory\Model\ResourceModel\Country\Collection;
  8. use Magento\Framework\Option\ArrayInterface;
  9. /**
  10. * Class Country
  11. */
  12. class Country implements ArrayInterface
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $options;
  18. /**
  19. * Countries
  20. *
  21. * @var \Magento\Directory\Model\ResourceModel\Country\Collection
  22. */
  23. protected $countryCollection;
  24. /**
  25. * Countries not supported by Braintree
  26. */
  27. protected $excludedCountries = [
  28. 'MM',
  29. 'IR',
  30. 'SD',
  31. 'BY',
  32. 'CI',
  33. 'CD',
  34. 'CG',
  35. 'IQ',
  36. 'LR',
  37. 'LB',
  38. 'KP',
  39. 'SL',
  40. 'SY',
  41. 'ZW',
  42. 'AL',
  43. 'BA',
  44. 'MK',
  45. 'ME',
  46. 'RS'
  47. ];
  48. /**
  49. * @param \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection
  50. */
  51. public function __construct(Collection $countryCollection)
  52. {
  53. $this->countryCollection = $countryCollection;
  54. }
  55. /**
  56. * @param bool $isMultiselect
  57. * @return array
  58. */
  59. public function toOptionArray($isMultiselect = false)
  60. {
  61. if (!$this->options) {
  62. $this->options = $this->countryCollection
  63. ->addFieldToFilter('country_id', ['nin' => $this->getExcludedCountries()])
  64. ->loadData()
  65. ->toOptionArray(false);
  66. }
  67. $options = $this->options;
  68. if (!$isMultiselect) {
  69. array_unshift($options, ['value' => '', 'label' => __('--Please Select--')]);
  70. }
  71. return $options;
  72. }
  73. /**
  74. * If country is in list of restricted (not supported by Braintree)
  75. *
  76. * @param string $countryId
  77. * @return boolean
  78. */
  79. public function isCountryRestricted($countryId)
  80. {
  81. return in_array($countryId, $this->getExcludedCountries());
  82. }
  83. /**
  84. * Return list of excluded countries
  85. * @return array
  86. */
  87. public function getExcludedCountries()
  88. {
  89. return $this->excludedCountries;
  90. }
  91. }