123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Data;
- /**
- * Class SearchResultProcessor
- */
- class SearchResultProcessor extends AbstractDataObject implements SearchResultProcessorInterface
- {
- /**
- * Data Interface name
- *
- * @var string
- */
- protected $dataInterface = \Magento\Framework\DataObject::class;
- /**
- * @var AbstractSearchResult
- */
- protected $searchResult;
- /**
- * @param AbstractSearchResult $searchResult
- */
- public function __construct(AbstractSearchResult $searchResult)
- {
- $this->searchResult = $searchResult;
- }
- /**
- * @return int
- */
- public function getCurrentPage()
- {
- return $this->searchResult->getSearchCriteria()->getLimit()[0];
- }
- /**
- * @return int
- */
- public function getPageSize()
- {
- return $this->searchResult->getSearchCriteria()->getLimit()[1];
- }
- /**
- * @return \Magento\Framework\DataObject|mixed
- */
- public function getFirstItem()
- {
- return current($this->searchResult->getItems());
- }
- /**
- * @return \Magento\Framework\DataObject|mixed
- */
- public function getLastItem()
- {
- $items = $this->searchResult->getItems();
- return end($items);
- }
- /**
- * @return array
- */
- public function getAllIds()
- {
- $ids = [];
- foreach ($this->searchResult->getItems() as $item) {
- $ids[] = $this->searchResult->getItemId($item);
- }
- return $ids;
- }
- /**
- * @param int $id
- * @return \Magento\Framework\DataObject|null
- */
- public function getItemById($id)
- {
- $items = $this->searchResult->getItems();
- if (isset($items[$id])) {
- return $items[$id];
- }
- return null;
- }
- /**
- * @param string $colName
- * @return array
- */
- public function getColumnValues($colName)
- {
- $col = [];
- foreach ($this->searchResult->getItems() as $item) {
- $col[] = $item->getData($colName);
- }
- return $col;
- }
- /**
- * @param string $column
- * @param mixed $value
- * @return array
- */
- public function getItemsByColumnValue($column, $value)
- {
- $res = [];
- foreach ($this->searchResult->getItems() as $item) {
- if ($item->getData($column) == $value) {
- $res[] = $item;
- }
- }
- return $res;
- }
- /**
- * @param string $column
- * @param mixed $value
- * @return \Magento\Framework\DataObject|null
- */
- public function getItemByColumnValue($column, $value)
- {
- foreach ($this->searchResult->getItems() as $item) {
- if ($item->getData($column) == $value) {
- return $item;
- }
- }
- return null;
- }
- /**
- * @param string $callback
- * @param array $args
- * @return array
- */
- public function walk($callback, array $args = [])
- {
- $results = [];
- $useItemCallback = is_string($callback) && strpos($callback, '::') === false;
- foreach ($this->searchResult->getItems() as $id => $item) {
- if ($useItemCallback) {
- $cb = [$item, $callback];
- } else {
- $cb = $callback;
- array_unshift($args, $item);
- }
- $results[$id] = call_user_func_array($cb, $args);
- }
- return $results;
- }
- /**
- * @return string
- */
- public function toXml()
- {
- $xml = '<?xml version="1.0" encoding="UTF-8"?>
- <collection>
- <totalRecords>' .
- $this->searchResult->getSize() .
- '</totalRecords>
- <items>';
- foreach ($this->searchResult->getItems() as $item) {
- $xml .= $item->toXml();
- }
- $xml .= '</items>
- </collection>';
- return $xml;
- }
- /**
- * @param array $arrRequiredFields
- * @return array
- */
- public function toArray($arrRequiredFields = [])
- {
- $array = [];
- $array['search_criteria'] = $this->searchResult->getSearchCriteria();
- $array['total_count'] = $this->searchResult->getTotalCount();
- foreach ($this->searchResult->getItems() as $item) {
- $array['items'][] = $item->toArray($arrRequiredFields);
- }
- return $array;
- }
- /**
- * @param string|null $valueField
- * @param string|null $labelField
- * @param array $additional
- * @return array
- */
- public function toOptionArray($valueField = null, $labelField = null, $additional = [])
- {
- if ($valueField === null) {
- $valueField = $this->searchResult->getIdFieldName();
- }
- if ($labelField === null) {
- $labelField = 'name';
- }
- $result = [];
- $additional['value'] = $valueField;
- $additional['label'] = $labelField;
- foreach ($this->searchResult->getItems() as $item) {
- $data = [];
- foreach ($additional as $code => $field) {
- $data[$code] = $item->getData($field);
- }
- $result[] = $data;
- }
- return $result;
- }
- /**
- * @param string $valueField
- * @param string $labelField
- * @return array
- */
- public function toOptionHash($valueField, $labelField)
- {
- $res = [];
- foreach ($this->searchResult->getItems() as $item) {
- $res[$item->getData($valueField)] = $item->getData($labelField);
- }
- return $res;
- }
- /**
- * @return string
- */
- protected function getDataInterfaceName()
- {
- return $this->dataInterface;
- }
- }
|