DistanceProvider.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryDistanceBasedSourceSelectionAdminUi\Model\Config\Source;
  8. use Magento\Framework\Option\ArrayInterface;
  9. class DistanceProvider implements ArrayInterface
  10. {
  11. /**
  12. * @var array|string[]
  13. */
  14. private $distanceProviderDescriptions;
  15. /**
  16. * DistanceProvider constructor.
  17. *
  18. * @param string[] $distanceProviderDescriptions
  19. * @SuppressWarnings(PHPMD.LongVariable)
  20. */
  21. public function __construct(
  22. array $distanceProviderDescriptions = []
  23. ) {
  24. $this->distanceProviderDescriptions = $distanceProviderDescriptions;
  25. }
  26. /**
  27. * Options getter
  28. *
  29. * @return array
  30. */
  31. public function toOptionArray()
  32. {
  33. $res = [];
  34. foreach ($this->distanceProviderDescriptions as $code => $description) {
  35. $res [] = [
  36. 'value' => $code,
  37. 'label' => $description
  38. ];
  39. }
  40. return $res;
  41. }
  42. /**
  43. * Get options in "key-value" format
  44. *
  45. * @return array
  46. */
  47. public function toArray()
  48. {
  49. $options = $this->toOptionArray();
  50. $return = [];
  51. foreach ($options as $option) {
  52. $return[$option['value']] = $option['label'];
  53. }
  54. return $return;
  55. }
  56. }