Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SearchResultProcessor.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\Data;
7 
12 {
18  protected $dataInterface = \Magento\Framework\DataObject::class;
19 
23  protected $searchResult;
24 
29  {
30  $this->searchResult = $searchResult;
31  }
32 
36  public function getCurrentPage()
37  {
38  return $this->searchResult->getSearchCriteria()->getLimit()[0];
39  }
40 
44  public function getPageSize()
45  {
46  return $this->searchResult->getSearchCriteria()->getLimit()[1];
47  }
48 
52  public function getFirstItem()
53  {
54  return current($this->searchResult->getItems());
55  }
56 
60  public function getLastItem()
61  {
62  $items = $this->searchResult->getItems();
63  return end($items);
64  }
65 
69  public function getAllIds()
70  {
71  $ids = [];
72  foreach ($this->searchResult->getItems() as $item) {
73  $ids[] = $this->searchResult->getItemId($item);
74  }
75  return $ids;
76  }
77 
82  public function getItemById($id)
83  {
84  $items = $this->searchResult->getItems();
85  if (isset($items[$id])) {
86  return $items[$id];
87  }
88  return null;
89  }
90 
95  public function getColumnValues($colName)
96  {
97  $col = [];
98  foreach ($this->searchResult->getItems() as $item) {
99  $col[] = $item->getData($colName);
100  }
101  return $col;
102  }
103 
109  public function getItemsByColumnValue($column, $value)
110  {
111  $res = [];
112  foreach ($this->searchResult->getItems() as $item) {
113  if ($item->getData($column) == $value) {
114  $res[] = $item;
115  }
116  }
117  return $res;
118  }
119 
125  public function getItemByColumnValue($column, $value)
126  {
127  foreach ($this->searchResult->getItems() as $item) {
128  if ($item->getData($column) == $value) {
129  return $item;
130  }
131  }
132  return null;
133  }
134 
140  public function walk($callback, array $args = [])
141  {
142  $results = [];
143  $useItemCallback = is_string($callback) && strpos($callback, '::') === false;
144  foreach ($this->searchResult->getItems() as $id => $item) {
145  if ($useItemCallback) {
146  $cb = [$item, $callback];
147  } else {
148  $cb = $callback;
149  array_unshift($args, $item);
150  }
151  $results[$id] = call_user_func_array($cb, $args);
152  }
153  return $results;
154  }
155 
159  public function toXml()
160  {
161  $xml = '<?xml version="1.0" encoding="UTF-8"?>
162  <collection>
163  <totalRecords>' .
164  $this->searchResult->getSize() .
165  '</totalRecords>
166  <items>';
167  foreach ($this->searchResult->getItems() as $item) {
168  $xml .= $item->toXml();
169  }
170  $xml .= '</items>
171  </collection>';
172  return $xml;
173  }
174 
179  public function toArray($arrRequiredFields = [])
180  {
181  $array = [];
182  $array['search_criteria'] = $this->searchResult->getSearchCriteria();
183  $array['total_count'] = $this->searchResult->getTotalCount();
184  foreach ($this->searchResult->getItems() as $item) {
185  $array['items'][] = $item->toArray($arrRequiredFields);
186  }
187  return $array;
188  }
189 
196  public function toOptionArray($valueField = null, $labelField = null, $additional = [])
197  {
198  if ($valueField === null) {
199  $valueField = $this->searchResult->getIdFieldName();
200  }
201  if ($labelField === null) {
202  $labelField = 'name';
203  }
204  $result = [];
205  $additional['value'] = $valueField;
206  $additional['label'] = $labelField;
207  foreach ($this->searchResult->getItems() as $item) {
208  $data = [];
209  foreach ($additional as $code => $field) {
210  $data[$code] = $item->getData($field);
211  }
212  $result[] = $data;
213  }
214  return $result;
215  }
216 
222  public function toOptionHash($valueField, $labelField)
223  {
224  $res = [];
225  foreach ($this->searchResult->getItems() as $item) {
226  $res[$item->getData($valueField)] = $item->getData($labelField);
227  }
228  return $res;
229  }
230 
234  protected function getDataInterfaceName()
235  {
236  return $this->dataInterface;
237  }
238 }
$results
Definition: popup.phtml:13
$id
Definition: fieldset.phtml:14
__construct(AbstractSearchResult $searchResult)
$value
Definition: gender.phtml:16
toOptionArray($valueField=null, $labelField=null, $additional=[])
$code
Definition: info.phtml:12
$items