Author.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model\Config\Source;
  9. /**
  10. * Authors list
  11. *
  12. */
  13. class Author implements \Magento\Framework\Option\ArrayInterface
  14. {
  15. /**
  16. * @var \Magento\User\Model\ResourceModel\User\CollectionFactory
  17. */
  18. protected $authorCollectionFactory;
  19. /**
  20. * @var array
  21. */
  22. protected $options;
  23. /**
  24. * Initialize dependencies.
  25. *
  26. * @param \Magento\User\Model\ResourceModel\User\CollectionFactory $authorCollectionFactory
  27. * @param void
  28. */
  29. public function __construct(
  30. \Magento\User\Model\ResourceModel\User\CollectionFactory $authorCollectionFactory
  31. ) {
  32. $this->authorCollectionFactory = $authorCollectionFactory;
  33. }
  34. /**
  35. * Options getter
  36. *
  37. * @return array
  38. */
  39. public function toOptionArray()
  40. {
  41. if ($this->options === null) {
  42. $this->options = [['label' => __('Please select'), 'value' => 0]];
  43. $collection = $this->authorCollectionFactory->create();
  44. foreach ($collection as $item) {
  45. $this->options[] = [
  46. 'label' => $item->getName(),
  47. 'value' => $item->getId(),
  48. ];
  49. }
  50. }
  51. return $this->options;
  52. }
  53. /**
  54. * Get options in "key-value" format
  55. *
  56. * @return array
  57. */
  58. public function toArray()
  59. {
  60. $array = [];
  61. foreach ($this->toOptionArray() as $item) {
  62. $array[$item['value']] = $item['label'];
  63. }
  64. return $array;
  65. }
  66. }