AbstractSearchResult.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data;
  7. /**
  8. * Class AbstractSearchResult
  9. */
  10. abstract class AbstractSearchResult extends AbstractDataObject implements SearchResultInterface
  11. {
  12. /**
  13. * Data Interface name
  14. *
  15. * @var string
  16. */
  17. protected $dataInterface = \Magento\Framework\DataObject::class;
  18. /**
  19. * Name prefix of events that are dispatched by model
  20. *
  21. * @var string
  22. */
  23. protected $eventPrefix = '';
  24. /**
  25. * Name of event parameter
  26. *
  27. * @var string
  28. */
  29. protected $eventObject = '';
  30. /**
  31. * Event manager proxy
  32. *
  33. * @var \Magento\Framework\Event\ManagerInterface
  34. */
  35. protected $eventManager = null;
  36. /**
  37. * Total items number
  38. *
  39. * @var int
  40. */
  41. protected $totalRecords;
  42. /**
  43. * Loading state flag
  44. *
  45. * @var bool
  46. */
  47. protected $isLoaded;
  48. /**
  49. * @var \Magento\Framework\Data\Collection\EntityFactoryInterface
  50. */
  51. protected $entityFactory;
  52. /**
  53. * @var \Magento\Framework\DB\QueryInterface
  54. */
  55. protected $query;
  56. /**
  57. * @var \Magento\Framework\DB\Select
  58. * @deprecated 101.0.0
  59. */
  60. protected $select;
  61. /**
  62. * @var \Magento\Framework\Data\SearchResultIteratorFactory
  63. */
  64. protected $resultIteratorFactory;
  65. /**
  66. * @param \Magento\Framework\DB\QueryInterface $query
  67. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  68. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  69. * @param \Magento\Framework\Data\SearchResultIteratorFactory $resultIteratorFactory
  70. */
  71. public function __construct(
  72. \Magento\Framework\DB\QueryInterface $query,
  73. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  74. \Magento\Framework\Event\ManagerInterface $eventManager,
  75. \Magento\Framework\Data\SearchResultIteratorFactory $resultIteratorFactory
  76. ) {
  77. $this->query = $query;
  78. $this->eventManager = $eventManager;
  79. $this->entityFactory = $entityFactory;
  80. $this->resultIteratorFactory = $resultIteratorFactory;
  81. $this->init();
  82. }
  83. /**
  84. * Standard query builder initialization
  85. *
  86. * @return void
  87. */
  88. abstract protected function init();
  89. /**
  90. * @return \Magento\Framework\DataObject[]
  91. */
  92. public function getItems()
  93. {
  94. $this->load();
  95. return $this->data['items'];
  96. }
  97. /**
  98. * @param \Magento\Framework\DataObject[] $items
  99. * @return $this
  100. */
  101. public function setItems(array $items = null)
  102. {
  103. $this->data['items'] = $items;
  104. return $this;
  105. }
  106. /**
  107. * @return int
  108. */
  109. public function getTotalCount()
  110. {
  111. if (!isset($this->data['total_count'])) {
  112. $this->data['total_count'] = $this->query->getSize();
  113. }
  114. return $this->data['total_count'];
  115. }
  116. /**
  117. * @param int $totalCount
  118. * @return $this
  119. */
  120. public function setTotalCount($totalCount)
  121. {
  122. $this->data['total_count'] = $totalCount;
  123. return $this;
  124. }
  125. /**
  126. * @return \Magento\Framework\Api\CriteriaInterface
  127. */
  128. public function getSearchCriteria()
  129. {
  130. if (!isset($this->data['search_criteria'])) {
  131. $this->data['search_criteria'] = $this->query->getCriteria();
  132. }
  133. return $this->data['search_criteria'];
  134. }
  135. /**
  136. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  137. * @return $this
  138. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  139. */
  140. public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
  141. {
  142. return $this;
  143. }
  144. /**
  145. * @return \Magento\Framework\Data\SearchResultIterator
  146. */
  147. public function createIterator()
  148. {
  149. return $this->resultIteratorFactory->create(
  150. [
  151. 'searchResult' => $this,
  152. 'query' => $this->query,
  153. ]
  154. );
  155. }
  156. /**
  157. * @param array $arguments
  158. * @return \Magento\Framework\DataObject|mixed
  159. */
  160. public function createDataObject(array $arguments = [])
  161. {
  162. return $this->entityFactory->create($this->getDataInterfaceName(), $arguments);
  163. }
  164. /**
  165. * @return string
  166. */
  167. public function getIdFieldName()
  168. {
  169. return $this->query->getIdFieldName();
  170. }
  171. /**
  172. * @return int
  173. */
  174. public function getSize()
  175. {
  176. return $this->query->getSize();
  177. }
  178. /**
  179. * Get collection item identifier
  180. *
  181. * @param \Magento\Framework\DataObject $item
  182. * @return mixed
  183. */
  184. public function getItemId(\Magento\Framework\DataObject $item)
  185. {
  186. $field = $this->query->getIdFieldName();
  187. if ($field) {
  188. return $item->getData($field);
  189. }
  190. return $item->getId();
  191. }
  192. /**
  193. * @return bool
  194. */
  195. protected function isLoaded()
  196. {
  197. return $this->isLoaded;
  198. }
  199. /**
  200. * Load data
  201. *
  202. * @return void
  203. */
  204. protected function load()
  205. {
  206. if (!$this->isLoaded()) {
  207. $this->beforeLoad();
  208. $data = $this->query->fetchAll();
  209. $this->data['items'] = [];
  210. if (is_array($data)) {
  211. foreach ($data as $row) {
  212. $item = $this->createDataObject(['data' => $row]);
  213. $this->addItem($item);
  214. }
  215. }
  216. $this->setIsLoaded(true);
  217. $this->afterLoad();
  218. }
  219. }
  220. /**
  221. * Set loading status flag
  222. *
  223. * @param bool $flag
  224. * @return void
  225. */
  226. protected function setIsLoaded($flag = true)
  227. {
  228. $this->isLoaded = $flag;
  229. }
  230. /**
  231. * Adding item to item array
  232. *
  233. * @param \Magento\Framework\DataObject $item
  234. * @return void
  235. * @throws \Exception
  236. */
  237. protected function addItem(\Magento\Framework\DataObject $item)
  238. {
  239. $itemId = $this->getItemId($item);
  240. if ($itemId !== null) {
  241. if (isset($this->data['items'][$itemId])) {
  242. throw new \Exception(
  243. 'Item (' . get_class($item) . ') with the same ID "' . $item->getId() . '" already exists.'
  244. );
  245. }
  246. $this->data['items'][$itemId] = $item;
  247. } else {
  248. $this->data['items'][] = $item;
  249. }
  250. }
  251. /**
  252. * Dispatch "before" load method events
  253. *
  254. * @return void
  255. */
  256. protected function beforeLoad()
  257. {
  258. $this->eventManager->dispatch('abstract_search_result_load_before', ['collection' => $this]);
  259. if ($this->eventPrefix && $this->eventObject) {
  260. $this->eventManager->dispatch($this->eventPrefix . '_load_before', [$this->eventObject => $this]);
  261. }
  262. }
  263. /**
  264. * Dispatch "after" load method events
  265. *
  266. * @return void
  267. */
  268. protected function afterLoad()
  269. {
  270. $this->eventManager->dispatch('abstract_search_result_load_after', ['collection' => $this]);
  271. if ($this->eventPrefix && $this->eventObject) {
  272. $this->eventManager->dispatch($this->eventPrefix . '_load_after', [$this->eventObject => $this]);
  273. }
  274. }
  275. /**
  276. * Set Data Interface name for collection items
  277. *
  278. * @param string $dataInterface
  279. * @return void
  280. */
  281. protected function setDataInterfaceName($dataInterface)
  282. {
  283. if (is_string($dataInterface)) {
  284. $this->dataInterface = $dataInterface;
  285. }
  286. }
  287. /**
  288. * Get Data Interface name for collection items
  289. *
  290. * @return string
  291. */
  292. protected function getDataInterfaceName()
  293. {
  294. return $this->dataInterface;
  295. }
  296. /**
  297. * @return \Magento\Framework\DB\QueryInterface
  298. */
  299. protected function getQuery()
  300. {
  301. return $this->query;
  302. }
  303. }