Group.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Customer\Source;
  7. use Magento\Customer\Api\Data\GroupSearchResultsInterface;
  8. use Magento\Framework\Module\Manager as ModuleManager;
  9. use Magento\Customer\Api\Data\GroupInterface;
  10. use Magento\Customer\Api\GroupRepositoryInterface;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. class Group implements GroupSourceInterface
  13. {
  14. /**
  15. * @var ModuleManager
  16. */
  17. protected $moduleManager;
  18. /**
  19. * @var GroupRepositoryInterface
  20. */
  21. protected $groupRepository;
  22. /**
  23. * @var SearchCriteriaBuilder
  24. */
  25. protected $searchCriteriaBuilder;
  26. /**
  27. * @param ModuleManager $moduleManager
  28. * @param GroupRepositoryInterface $groupRepository
  29. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  30. */
  31. public function __construct(
  32. ModuleManager $moduleManager,
  33. GroupRepositoryInterface $groupRepository,
  34. SearchCriteriaBuilder $searchCriteriaBuilder
  35. ) {
  36. $this->moduleManager = $moduleManager;
  37. $this->groupRepository = $groupRepository;
  38. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  39. }
  40. /**
  41. * Return array of customer groups
  42. *
  43. * @return array
  44. */
  45. public function toOptionArray()
  46. {
  47. if (!$this->moduleManager->isEnabled('Magento_Customer')) {
  48. return [];
  49. }
  50. $customerGroups = [];
  51. $customerGroups[] = [
  52. 'label' => __('ALL GROUPS'),
  53. 'value' => (string)GroupInterface::CUST_GROUP_ALL,
  54. ];
  55. /** @var GroupSearchResultsInterface $groups */
  56. $groups = $this->groupRepository->getList($this->searchCriteriaBuilder->create());
  57. foreach ($groups->getItems() as $group) {
  58. $customerGroups[] = [
  59. 'label' => $group->getCode(),
  60. 'value' => $group->getId(),
  61. ];
  62. }
  63. return $customerGroups;
  64. }
  65. }