-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProductEntity.php
More file actions
122 lines (100 loc) · 3.91 KB
/
Copy pathProductEntity.php
File metadata and controls
122 lines (100 loc) · 3.91 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
<?php
declare(strict_types=1);
namespace Omikron\FactFinder\Shopware6\Export\Data\Entity;
use Omikron\FactFinder\Shopware6\Export\Data\ExportEntityInterface;
use Omikron\FactFinder\Shopware6\Export\Field\FieldInterface;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Content\Product\ProductEntity as Product;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
use function Omikron\FactFinder\Shopware6\Internal\Utils\safeGetByName;
class ProductEntity implements ExportEntityInterface, ProductEntityInterface
{
private Product $product;
private ?Product $parent = null;
private string $filterAttributes = '';
private string $customFields = '';
private \Traversable $additionalCache;
/** @var FieldInterface[] */
private iterable $productFields;
/** @var FieldInterface[] */
private iterable $cachedProductFields;
public function __construct(
Product $product,
\Traversable $productFields,
\Traversable $cachedProductFields,
) {
$this->product = $product;
$this->productFields = iterator_to_array($productFields);
$this->cachedProductFields = $cachedProductFields;
$this->additionalCache = new \ArrayIterator();
}
public function getId(): string
{
return $this->product->getId();
}
public function getChildren(): ?ProductCollection
{
return $this->product->getChildren();
}
public function getProperties(): ?PropertyGroupOptionCollection
{
return $this->product->getProperties();
}
public function getProductNumber(): string
{
return $this->product->getProductNumber();
}
public function getProductName(): string
{
return (string) $this->product->getTranslation('name');
}
public function getFilterAttributes(): string
{
return $this->filterAttributes;
}
public function getAdditionalCache(string $key): ?string
{
return safeGetByName(iterator_to_array($this->additionalCache), $key);
}
public function setFilterAttributes(string $filterAttributes): void
{
$this->filterAttributes = $filterAttributes;
}
public function setAdditionalCache(\Traversable $additionalCache): void
{
$this->additionalCache = $additionalCache;
}
public function getCustomFields(): string
{
return $this->customFields;
}
public function setCustomFields(string $customFields): void
{
$this->customFields = $customFields;
}
public function setParent(?Product $parent): void
{
$this->parent = $parent;
}
public function toArray(): array
{
$cachedProductFieldNames = array_map(fn (FieldInterface $field) => $field->getName(), iterator_to_array($this->cachedProductFields));
$fields = array_filter($this->productFields, fn (FieldInterface $productField) => !in_array($productField->getName(), $cachedProductFieldNames));
$isVariant = $this->product->getId() !== $this->product->getParentId() && isset($this->parent);
$defaultFields = [
'ProductNumber' => $this->product->getProductNumber(),
'Master' => $isVariant ? $this->parent->getProductNumber() : $this->product->getProductNumber(),
'Name' => $this->getProductName(),
'FilterAttributes' => $this->getFilterAttributes(),
'CustomFields' => $this->getCustomFields(),
];
return array_reduce(
$fields,
fn (array $fields, FieldInterface $field): array => array_merge(
$fields,
[$field->getName() => ($this->getAdditionalCache($field->getName()) ?? $field->getValue($isVariant ? $this->parent : $this->product))]
),
$defaultFields
);
}
}