|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Mage-OS Team. All rights reserved. |
| 4 | + * See LICENSE for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace MageOS\Indexer\Model; |
| 10 | + |
| 11 | +use MageOS\Indexer\Api\IndexRecordMutableData; |
| 12 | + |
| 13 | +use Traversable; |
| 14 | + |
| 15 | +class ArrayIndexRecordData implements \IteratorAggregate, IndexRecordMutableData |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + private array $data = [], |
| 19 | + private array $scopeData = [] |
| 20 | + ) { |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + public function getIterator(): Traversable |
| 25 | + { |
| 26 | + foreach ($this->data as $entityId => $item) { |
| 27 | + yield $entityId; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public function reset(): void |
| 32 | + { |
| 33 | + $this->data = []; |
| 34 | + $this->scopeData = []; |
| 35 | + } |
| 36 | + |
| 37 | + public function setValue(int $entityId, array $data): void |
| 38 | + { |
| 39 | + $this->data[$entityId] = $data; |
| 40 | + } |
| 41 | + |
| 42 | + public function addValue(int $entityId, string $field, mixed $value): void |
| 43 | + { |
| 44 | + $this->data[$entityId][$field] = $value; |
| 45 | + } |
| 46 | + |
| 47 | + public function extendValue(int $entityId, string $field, string $key, mixed $value): void |
| 48 | + { |
| 49 | + $this->data[$entityId][$field][$key] = $value; |
| 50 | + } |
| 51 | + |
| 52 | + public function addScopeValue(int $entityId, int $storeId, string $field, mixed $value): void |
| 53 | + { |
| 54 | + if (!isset($this->data[$entityId])) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $this->scopeData[$entityId][$storeId][$field] = $value; |
| 59 | + } |
| 60 | + |
| 61 | + public function getScopeValue(int $entityId, int $scopeId, string $field): mixed |
| 62 | + { |
| 63 | + return $this->scopeData[$entityId][$scopeId][$field] |
| 64 | + ?? $this->scopeData[$entityId][0][$field] |
| 65 | + ?? null; |
| 66 | + } |
| 67 | + |
| 68 | + public function getValue(int $entityId, string $field): mixed |
| 69 | + { |
| 70 | + return $this->data[$entityId][$field] ?? null; |
| 71 | + } |
| 72 | +} |
0 commit comments