SearchResultProcessor.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 SearchResultProcessor
  9. */
  10. class SearchResultProcessor extends AbstractDataObject implements SearchResultProcessorInterface
  11. {
  12. /**
  13. * Data Interface name
  14. *
  15. * @var string
  16. */
  17. protected $dataInterface = \Magento\Framework\DataObject::class;
  18. /**
  19. * @var AbstractSearchResult
  20. */
  21. protected $searchResult;
  22. /**
  23. * @param AbstractSearchResult $searchResult
  24. */
  25. public function __construct(AbstractSearchResult $searchResult)
  26. {
  27. $this->searchResult = $searchResult;
  28. }
  29. /**
  30. * @return int
  31. */
  32. public function getCurrentPage()
  33. {
  34. return $this->searchResult->getSearchCriteria()->getLimit()[0];
  35. }
  36. /**
  37. * @return int
  38. */
  39. public function getPageSize()
  40. {
  41. return $this->searchResult->getSearchCriteria()->getLimit()[1];
  42. }
  43. /**
  44. * @return \Magento\Framework\DataObject|mixed
  45. */
  46. public function getFirstItem()
  47. {
  48. return current($this->searchResult->getItems());
  49. }
  50. /**
  51. * @return \Magento\Framework\DataObject|mixed
  52. */
  53. public function getLastItem()
  54. {
  55. $items = $this->searchResult->getItems();
  56. return end($items);
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getAllIds()
  62. {
  63. $ids = [];
  64. foreach ($this->searchResult->getItems() as $item) {
  65. $ids[] = $this->searchResult->getItemId($item);
  66. }
  67. return $ids;
  68. }
  69. /**
  70. * @param int $id
  71. * @return \Magento\Framework\DataObject|null
  72. */
  73. public function getItemById($id)
  74. {
  75. $items = $this->searchResult->getItems();
  76. if (isset($items[$id])) {
  77. return $items[$id];
  78. }
  79. return null;
  80. }
  81. /**
  82. * @param string $colName
  83. * @return array
  84. */
  85. public function getColumnValues($colName)
  86. {
  87. $col = [];
  88. foreach ($this->searchResult->getItems() as $item) {
  89. $col[] = $item->getData($colName);
  90. }
  91. return $col;
  92. }
  93. /**
  94. * @param string $column
  95. * @param mixed $value
  96. * @return array
  97. */
  98. public function getItemsByColumnValue($column, $value)
  99. {
  100. $res = [];
  101. foreach ($this->searchResult->getItems() as $item) {
  102. if ($item->getData($column) == $value) {
  103. $res[] = $item;
  104. }
  105. }
  106. return $res;
  107. }
  108. /**
  109. * @param string $column
  110. * @param mixed $value
  111. * @return \Magento\Framework\DataObject|null
  112. */
  113. public function getItemByColumnValue($column, $value)
  114. {
  115. foreach ($this->searchResult->getItems() as $item) {
  116. if ($item->getData($column) == $value) {
  117. return $item;
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * @param string $callback
  124. * @param array $args
  125. * @return array
  126. */
  127. public function walk($callback, array $args = [])
  128. {
  129. $results = [];
  130. $useItemCallback = is_string($callback) && strpos($callback, '::') === false;
  131. foreach ($this->searchResult->getItems() as $id => $item) {
  132. if ($useItemCallback) {
  133. $cb = [$item, $callback];
  134. } else {
  135. $cb = $callback;
  136. array_unshift($args, $item);
  137. }
  138. $results[$id] = call_user_func_array($cb, $args);
  139. }
  140. return $results;
  141. }
  142. /**
  143. * @return string
  144. */
  145. public function toXml()
  146. {
  147. $xml = '<?xml version="1.0" encoding="UTF-8"?>
  148. <collection>
  149. <totalRecords>' .
  150. $this->searchResult->getSize() .
  151. '</totalRecords>
  152. <items>';
  153. foreach ($this->searchResult->getItems() as $item) {
  154. $xml .= $item->toXml();
  155. }
  156. $xml .= '</items>
  157. </collection>';
  158. return $xml;
  159. }
  160. /**
  161. * @param array $arrRequiredFields
  162. * @return array
  163. */
  164. public function toArray($arrRequiredFields = [])
  165. {
  166. $array = [];
  167. $array['search_criteria'] = $this->searchResult->getSearchCriteria();
  168. $array['total_count'] = $this->searchResult->getTotalCount();
  169. foreach ($this->searchResult->getItems() as $item) {
  170. $array['items'][] = $item->toArray($arrRequiredFields);
  171. }
  172. return $array;
  173. }
  174. /**
  175. * @param string|null $valueField
  176. * @param string|null $labelField
  177. * @param array $additional
  178. * @return array
  179. */
  180. public function toOptionArray($valueField = null, $labelField = null, $additional = [])
  181. {
  182. if ($valueField === null) {
  183. $valueField = $this->searchResult->getIdFieldName();
  184. }
  185. if ($labelField === null) {
  186. $labelField = 'name';
  187. }
  188. $result = [];
  189. $additional['value'] = $valueField;
  190. $additional['label'] = $labelField;
  191. foreach ($this->searchResult->getItems() as $item) {
  192. $data = [];
  193. foreach ($additional as $code => $field) {
  194. $data[$code] = $item->getData($field);
  195. }
  196. $result[] = $data;
  197. }
  198. return $result;
  199. }
  200. /**
  201. * @param string $valueField
  202. * @param string $labelField
  203. * @return array
  204. */
  205. public function toOptionHash($valueField, $labelField)
  206. {
  207. $res = [];
  208. foreach ($this->searchResult->getItems() as $item) {
  209. $res[$item->getData($valueField)] = $item->getData($labelField);
  210. }
  211. return $res;
  212. }
  213. /**
  214. * @return string
  215. */
  216. protected function getDataInterfaceName()
  217. {
  218. return $this->dataInterface;
  219. }
  220. }