forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchResult.php
More file actions
254 lines (232 loc) · 6.61 KB
/
SearchResult.php
File metadata and controls
254 lines (232 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Element\UiComponent\DataProvider;
use Magento\Framework\Api;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Psr\Log\LoggerInterface as Logger;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\ObjectManager;
/**
* Class SearchResult
* Generic Search Result
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @api
*/
class SearchResult extends AbstractCollection implements Api\Search\SearchResultInterface
{
/**
* @var Api\Search\AggregationInterface
*/
protected $aggregations;
/**
* @var Api\Search\SearchCriteriaInterface
*/
protected $searchCriteria;
/**
* @var int
*/
protected $totalCount;
/**
* @var string class name of document
*/
protected $document = Document::class;
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var string
*/
private $identifierName;
/**
* SearchResult constructor.
* @param EntityFactory $entityFactory
* @param Logger $logger
* @param FetchStrategy $fetchStrategy
* @param EventManager $eventManager
* @param string $mainTable
* @param null|string $resourceModel
* @param null|string $identifierName
* @param null|string $connectionName
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function __construct(
EntityFactory $entityFactory,
Logger $logger,
FetchStrategy $fetchStrategy,
EventManager $eventManager,
$mainTable,
$resourceModel = null,
$identifierName = null,
$connectionName = null
) {
$this->_init($this->document, $resourceModel);
$this->setMainTable(true);
if ($connectionName) {
$connection = $this->getResourceConnection()->getConnectionByName($connectionName);
} else {
$connection = $this->getResourceConnection()->getConnection();
}
$this->setMainTable($this->getResourceConnection()->getTableName($mainTable));
$this->identifierName = $identifierName;
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$connection,
null
);
$this->_setIdFieldName($this->getIdentifierName());
}
/**
* @deprecated 101.0.0
* @return ResourceConnection
*/
private function getResourceConnection()
{
if ($this->resourceConnection == null) {
$this->resourceConnection = ObjectManager::getInstance()->get(ResourceConnection::class);
}
return $this->resourceConnection;
}
/**
* @return \Magento\Framework\Api\Search\AggregationInterface
*/
public function getAggregations()
{
return $this->aggregations;
}
/**
* Get resource instance
*
* @return ResourceConnection|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
public function getResource()
{
if ($this->_resourceModel) {
return parent::getResource();
}
return $this->getResourceConnection();
}
/**
* @param \Magento\Framework\Api\Search\AggregationInterface $aggregations
* @return void
*/
public function setAggregations($aggregations)
{
$this->aggregations = $aggregations;
}
/**
* @return \Magento\Framework\Api\Search\SearchCriteriaInterface|null
*/
public function getSearchCriteria()
{
return $this->searchCriteria;
}
/**
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
$this->searchCriteria = $searchCriteria;
return $this;
}
/**
* @return int
*/
public function getTotalCount()
{
if (!$this->totalCount) {
$this->totalCount = $this->getSize();
}
return $this->totalCount;
}
/**
* @param int $totalCount
* @return $this
*/
public function setTotalCount($totalCount)
{
$this->totalCount = $totalCount;
return $this;
}
/**
* Set items list.
*
* @param Document[] $items
* @return $this
*/
public function setItems(array $items = null)
{
if ($items) {
foreach ($items as $item) {
$this->addItem($item);
}
unset($this->totalCount);
}
return $this;
}
/**
* Retrieve table name
*
* @param string $table
* @return string
*/
public function getTable($table)
{
return $this->getResourceConnection()->getTableName($table);
}
/**
* Get Identifier name
*
* @return string|bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getIdentifierName()
{
if ($this->_resourceModel) {
return $this->getResource()->getIdFieldName();
} elseif ($this->identifierName !== null) {
return $this->identifierName;
} else {
return $this->getResourceConnection()->getConnection()->getAutoIncrementField($this->getMainTable());
}
}
/**
* Initialize initial fields to select like id field
*
* @return $this
*/
protected function _initInitialFieldsToSelect()
{
$idFieldName = $this->getIdentifierName();
if ($idFieldName) {
$this->_initialFieldsToSelect[] = $idFieldName;
}
return $this;
}
/**
* Retrieve all ids for collection
*
* @return array
*/
public function getAllIds()
{
$idsSelect = clone $this->getSelect();
$idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
$idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
$idsSelect->columns($this->getIdentifierName(), 'main_table');
return $this->getConnection()->fetchCol($idsSelect, $this->_bindParams);
}
}