1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Factory; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ProductImageInterface; |
9
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
10
|
|
|
use Sylius\Component\Core\Model\ProductTranslationInterface; |
11
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
12
|
|
|
use Sylius\ShopApiPlugin\View\ProductTaxonView; |
13
|
|
|
use Sylius\ShopApiPlugin\View\ProductView; |
14
|
|
|
|
15
|
|
|
final class ProductViewFactory implements ProductViewFactoryInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ImageViewFactoryInterface */ |
18
|
|
|
private $imageViewFactory; |
19
|
|
|
|
20
|
|
|
/** @var ProductAttributeValuesViewFactoryInterface */ |
21
|
|
|
private $attributeValuesViewFactory; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $productViewClass; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $productTaxonViewClass; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $fallbackLocale; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
ImageViewFactoryInterface $imageViewFactory, |
34
|
|
|
ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory, |
35
|
|
|
string $productViewClass, |
36
|
|
|
string $productTaxonViewClass, |
37
|
|
|
string $fallbackLocale |
38
|
|
|
) { |
39
|
|
|
$this->imageViewFactory = $imageViewFactory; |
40
|
|
|
$this->attributeValuesViewFactory = $attributeValuesViewFactory; |
41
|
|
|
$this->productViewClass = $productViewClass; |
42
|
|
|
$this->productTaxonViewClass = $productTaxonViewClass; |
43
|
|
|
$this->fallbackLocale = $fallbackLocale; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function create(ProductInterface $product, ChannelInterface $channel, string $locale): ProductView |
50
|
|
|
{ |
51
|
|
|
/** @var ProductView $productView */ |
52
|
|
|
$productView = new $this->productViewClass(); |
53
|
|
|
$productView->code = $product->getCode(); |
54
|
|
|
$productView->averageRating = $product->getAverageRating(); |
55
|
|
|
|
56
|
|
|
/** @var ProductTranslationInterface $translation */ |
57
|
|
|
$translation = $product->getTranslation($locale); |
58
|
|
|
$productView->name = $translation->getName(); |
59
|
|
|
$productView->slug = $translation->getSlug(); |
60
|
|
|
$productView->description = $translation->getDescription(); |
61
|
|
|
$productView->shortDescription = $translation->getShortDescription(); |
62
|
|
|
$productView->metaKeywords = $translation->getMetaKeywords(); |
63
|
|
|
$productView->metaDescription = $translation->getMetaDescription(); |
64
|
|
|
|
65
|
|
|
/** @var ProductImageInterface $image */ |
66
|
|
|
foreach ($product->getImages() as $image) { |
67
|
|
|
$imageView = $this->imageViewFactory->create($image); |
68
|
|
|
$productView->images[] = $imageView; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var ProductTaxonView $taxons */ |
72
|
|
|
$taxons = new $this->productTaxonViewClass(); |
73
|
|
|
if (null !== $product->getMainTaxon()) { |
74
|
|
|
$taxons->main = $product->getMainTaxon()->getCode(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @var TaxonInterface $taxon */ |
78
|
|
|
foreach ($product->getTaxons() as $taxon) { |
79
|
|
|
$taxons->others[] = $taxon->getCode(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$productView->taxons = $taxons; |
83
|
|
|
|
84
|
|
|
$productView->attributes = $this->attributeValuesViewFactory->create( |
85
|
|
|
$product->getAttributesByLocale($locale, $this->fallbackLocale)->toArray(), |
86
|
|
|
$locale |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $productView; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|