Source.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Rate;
  7. use Magento\Framework\Api\SearchCriteriaBuilder;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Convert\DataObject as Converter;
  10. use Magento\Tax\Api\TaxRateRepositoryInterface;
  11. use Magento\Tax\Model\Rate\Provider as RateProvider;
  12. /**
  13. * Tax rate source model.
  14. */
  15. class Source implements \Magento\Framework\Data\OptionSourceInterface
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $options;
  21. /**
  22. * @var \Magento\Tax\Api\TaxRateRepositoryInterface
  23. */
  24. protected $taxRateRepository;
  25. /**
  26. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  27. */
  28. protected $searchCriteriaBuilder;
  29. /**
  30. * @var \Magento\Framework\Convert\DataObject
  31. */
  32. protected $converter;
  33. /**
  34. * @var \Magento\Tax\Model\Rate\Provider
  35. */
  36. protected $rateProvider;
  37. /**
  38. * Initialize dependencies.
  39. *
  40. * @param TaxRateRepositoryInterface $taxRateRepository
  41. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  42. * @param Converter $converter
  43. * @param RateProvider $rateProvider
  44. */
  45. public function __construct(
  46. TaxRateRepositoryInterface $taxRateRepository,
  47. SearchCriteriaBuilder $searchCriteriaBuilder,
  48. Converter $converter,
  49. RateProvider $rateProvider = null
  50. ) {
  51. $this->taxRateRepository = $taxRateRepository;
  52. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  53. $this->converter = $converter;
  54. $this->rateProvider = $rateProvider ?: ObjectManager::getInstance()->get(RateProvider::class);
  55. }
  56. /**
  57. * Retrieve all tax rates as an options array.
  58. *
  59. * @return array
  60. */
  61. public function toOptionArray()
  62. {
  63. if (!$this->options) {
  64. $searchCriteria = $this->searchCriteriaBuilder
  65. ->setPageSize($this->rateProvider->getPageSize())
  66. ->setCurrentPage(1)
  67. ->create();
  68. $this->options = $this->rateProvider->toOptionArray($searchCriteria);
  69. }
  70. return $this->options;
  71. }
  72. }