Country.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 countries list
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Country implements \Magento\Framework\Option\ArrayInterface
  14. {
  15. /**
  16. * Countries
  17. *
  18. * @var \Magento\Directory\Model\ResourceModel\Country\Collection
  19. */
  20. protected $_countryCollection;
  21. /**
  22. * Options array
  23. *
  24. * @var array
  25. */
  26. protected $_options;
  27. /**
  28. * @param \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection
  29. */
  30. public function __construct(\Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection)
  31. {
  32. $this->_countryCollection = $countryCollection;
  33. }
  34. /**
  35. * Return options array
  36. *
  37. * @param boolean $isMultiselect
  38. * @param string|array $foregroundCountries
  39. * @return array
  40. */
  41. public function toOptionArray($isMultiselect = false, $foregroundCountries = '')
  42. {
  43. if (!$this->_options) {
  44. $this->_options = $this->_countryCollection->loadData()->setForegroundCountries(
  45. $foregroundCountries
  46. )->toOptionArray(
  47. false
  48. );
  49. }
  50. $options = $this->_options;
  51. if (!$isMultiselect) {
  52. array_unshift($options, ['value' => '', 'label' => __('--Please Select--')]);
  53. }
  54. return $options;
  55. }
  56. }