PickupLocationSearchResult.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Delivery;
  6. use Magento\Framework\Api\SearchCriteriaInterface;
  7. use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
  8. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchResultInterface;
  9. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  10. use Temando\Shipping\Model\Delivery\QuotePickupLocation;
  11. use Temando\Shipping\Model\ResourceModel\Delivery\QuotePickupLocation as QuotePickupLocationResource;
  12. /**
  13. * Pickup location collection
  14. *
  15. * @package Temando\Shipping\Model
  16. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  17. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link https://www.temando.com/
  19. */
  20. class PickupLocationSearchResult extends AbstractCollection implements PickupLocationSearchResultInterface
  21. {
  22. /**
  23. * @var \Magento\Framework\Api\SearchCriteriaInterface
  24. */
  25. private $searchCriteria;
  26. /**
  27. * Event prefix
  28. *
  29. * @var string
  30. */
  31. protected $_eventPrefix = 'temando_pickup_location_collection';
  32. /**
  33. * Event object
  34. *
  35. * @var string
  36. */
  37. protected $_eventObject = 'pickup_location_collection';
  38. /**
  39. * Init collection and determine table names
  40. *
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. $this->_init(QuotePickupLocation::class, QuotePickupLocationResource::class);
  46. }
  47. /**
  48. * Get search criteria.
  49. *
  50. * @return \Magento\Framework\Api\SearchCriteriaInterface|null
  51. */
  52. public function getSearchCriteria()
  53. {
  54. return $this->searchCriteria;
  55. }
  56. /**
  57. * Set search criteria.
  58. *
  59. * @param SearchCriteriaInterface $searchCriteria
  60. * @return $this
  61. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  62. */
  63. public function setSearchCriteria(SearchCriteriaInterface $searchCriteria = null)
  64. {
  65. $this->searchCriteria = $searchCriteria;
  66. return $this;
  67. }
  68. /**
  69. * Get total count.
  70. *
  71. * @return int
  72. */
  73. public function getTotalCount()
  74. {
  75. return $this->getSize();
  76. }
  77. /**
  78. * Not applicable, Collection vs. Search Result seems to be work in progress.
  79. *
  80. * @param int $totalCount
  81. * @return $this
  82. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  83. */
  84. public function setTotalCount($totalCount)
  85. {
  86. return $this;
  87. }
  88. /**
  89. * Set items list.
  90. *
  91. * @param QuotePickupLocationInterface[] $items
  92. * @return $this
  93. * @throws \Exception
  94. */
  95. public function setItems(array $items = null)
  96. {
  97. if (!$items) {
  98. return $this;
  99. }
  100. foreach ($items as $item) {
  101. $this->addItem($item);
  102. }
  103. return $this;
  104. }
  105. /**
  106. * Unserialize opening_hours in each item
  107. *
  108. * @return AbstractCollection
  109. */
  110. protected function _afterLoad()
  111. {
  112. /** @var QuotePickupLocation $item */
  113. foreach ($this->_items as $item) {
  114. if (is_string($item->getOpeningHours())) {
  115. $this->getResource()->unserializeFields($item);
  116. }
  117. // as of v1.3.0 the serialized data structure changed
  118. $openingHours = $item->getOpeningHours();
  119. if (is_array($openingHours) && !array_key_exists('general', $openingHours)) {
  120. $openingHours['general'] = $openingHours;
  121. $openingHours['specific'] = [];
  122. $item->setData(QuotePickupLocationInterface::OPENING_HOURS, $openingHours);
  123. }
  124. // cast values for type safety
  125. $distance = $item->getDistance() ? (int) $item->getDistance() : null;
  126. $item->setData(QuotePickupLocationInterface::DISTANCE, $distance);
  127. $item->setData(QuotePickupLocationInterface::SELECTED, (bool) $item->isSelected());
  128. }
  129. parent::_afterLoad();
  130. return $this;
  131. }
  132. }