Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexIterator.php
Go to the documentation of this file.
1 <?php
8 
21 class IndexIterator implements \Iterator
22 {
26  private $dataProvider;
27 
31  private $storeId;
32 
36  private $staticFields;
37 
41  private $productIds;
42 
46  private $dynamicFields;
47 
51  private $visibility;
52 
56  private $allowedVisibility;
57 
61  private $status;
62 
66  private $statusIds;
67 
71  private $lastProductId = 0;
72 
76  private $products = [];
77 
81  private $current = null;
82 
86  private $isValid = true;
87 
91  private $key = null;
92 
96  private $productAttributes = [];
97 
101  private $productRelations = [];
102 
118  public function __construct(
119  DataProvider $dataProvider,
120  $storeId,
121  array $staticFields,
122  $productIds,
123  array $dynamicFields,
124  \Magento\Eav\Model\Entity\Attribute $visibility,
125  array $allowedVisibility,
126  \Magento\Eav\Model\Entity\Attribute $status,
127  array $statusIds
128  ) {
129  $this->dataProvider = $dataProvider;
130  $this->storeId = $storeId;
131  $this->staticFields = $staticFields;
132  $this->productIds = $productIds;
133  $this->dynamicFields = $dynamicFields;
134  $this->visibility = $visibility;
135  $this->allowedVisibility = $allowedVisibility;
136  $this->status = $status;
137  $this->statusIds = $statusIds;
138  }
139 
146  public function current()
147  {
148  return $this->current;
149  }
150 
157  public function next()
158  {
159  \next($this->products);
160  if (\key($this->products) === null) {
161  // check if storage has more items to process
162  $this->products = $this->dataProvider->getSearchableProducts(
163  $this->storeId,
164  $this->staticFields,
165  $this->productIds,
166  $this->lastProductId
167  );
168 
169  if (!count($this->products)) {
170  $this->isValid = false;
171  return;
172  }
173 
174  $productAttributes = [];
175  $this->productRelations = [];
176  foreach ($this->products as $productData) {
177  $this->lastProductId = $productData['entity_id'];
178  $productAttributes[$productData['entity_id']] = $productData['entity_id'];
179  $productChildren = $this->dataProvider->getProductChildIds(
180  $productData['entity_id'],
181  $productData['type_id']
182  );
183  $this->productRelations[$productData['entity_id']] = $productChildren;
184  if ($productChildren) {
185  foreach ($productChildren as $productChildId) {
186  $productAttributes[$productChildId] = $productChildId;
187  }
188  }
189  }
190  \reset($this->products);
191 
192  $this->productAttributes = $this->dataProvider->getProductAttributes(
193  $this->storeId,
194  $productAttributes,
195  $this->dynamicFields
196  );
197  }
198 
199  $productData = \current($this->products);
200 
201  if (!isset($this->productAttributes[$productData['entity_id']])) {
202  $this->next();
203  return;
204  }
205 
206  $productAttr = $this->productAttributes[$productData['entity_id']];
207  if (!isset($productAttr[$this->visibility->getId()])
208  || !in_array($productAttr[$this->visibility->getId()], $this->allowedVisibility)
209  ) {
210  $this->next();
211  return;
212  }
213  if (!isset($productAttr[$this->status->getId()])
214  || !in_array($productAttr[$this->status->getId()], $this->statusIds)
215  ) {
216  $this->next();
217  return;
218  }
219 
220  $productIndex = [$productData['entity_id'] => $productAttr];
221 
222  $hasChildren = false;
223  $productChildren = $this->productRelations[$productData['entity_id']];
224  if ($productChildren) {
225  foreach ($productChildren as $productChildId) {
226  if (isset($this->productAttributes[$productChildId])) {
227  $productChildAttr = $this->productAttributes[$productChildId];
228  if (!isset($productChildAttr[$this->status->getId()])
229  || !in_array($productChildAttr[$this->status->getId()], $this->statusIds)
230  ) {
231  continue;
232  }
233 
234  $hasChildren = true;
235  $productIndex[$productChildId] = $productChildAttr;
236  }
237  }
238  }
239  if ($productChildren !== null && !$hasChildren) {
240  $this->next();
241  return;
242  }
243 
244  $index = $this->dataProvider->prepareProductIndex($productIndex, $productData, $this->storeId);
245 
246  $this->current = $index;
247  $this->key = $productData['entity_id'];
248  }
249 
256  public function key()
257  {
258  return $this->key;
259  }
260 
267  public function valid()
268  {
269  return $this->isValid;
270  }
271 
278  public function rewind()
279  {
280  $this->lastProductId = 0;
281  $this->key = null;
282  $this->current = null;
283  unset($this->products);
284  $this->products = [];
285  $this->next();
286  }
287 }
$status
Definition: order_status.php:8
$productData
__construct(DataProvider $dataProvider, $storeId, array $staticFields, $productIds, array $dynamicFields, \Magento\Eav\Model\Entity\Attribute $visibility, array $allowedVisibility, \Magento\Eav\Model\Entity\Attribute $status, array $statusIds)
$index
Definition: list.phtml:44