-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVariantEntity.php
More file actions
55 lines (44 loc) · 1.87 KB
/
Copy pathVariantEntity.php
File metadata and controls
55 lines (44 loc) · 1.87 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
<?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 Omikron\FactFinder\Shopware6\Export\PropertyFormatter;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity as Product;
use function array_map as map;
class VariantEntity implements ExportEntityInterface, ProductEntityInterface
{
private Product $product;
private array $parentData;
private PropertyFormatter $propertyFormatter;
/** @var FieldInterface[] */
private iterable $variantFields;
public function __construct(
Product $product,
array $parentData,
PropertyFormatter $propertyFormatter,
iterable $variantFields,
) {
$this->product = $product;
$this->parentData = $parentData;
$this->propertyFormatter = $propertyFormatter;
$this->variantFields = $variantFields;
}
public function getId(): string
{
return $this->product->getId();
}
public function getProductNumber(): string
{
return $this->product->getProductNumber();
}
public function getProductName(): string
{
return $this->product->getTranslation('name') ? (string) $this->product->getTranslation('name') : $this->parentData['Name'];
}
public function toArray(): array
{
$opts = '|' . implode('|', map($this->propertyFormatter, $this->product->getOptions()->getElements())) . '|';
return array_reduce($this->variantFields, fn (array $fields, FieldInterface $field): array => [$field->getName() => $field->getValue($this->product)] + $fields, ['ProductNumber' => $this->product->getProductNumber(), 'Name' => $this->getProductName(), 'FilterAttributes' => $opts] + $this->parentData);
}
}