|
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\ProductView; |
|
13
|
|
|
use Sylius\ShopApiPlugin\View\TaxonView; |
|
14
|
|
|
|
|
15
|
|
|
final class ProductViewFactory implements ProductViewFactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ImageViewFactoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $imageViewFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ProductAttributeValuesViewFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $attributeValuesViewFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TaxonViewFactoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $taxonViewFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $fallback; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ImageViewFactoryInterface $imageViewFactory |
|
39
|
|
|
* @param ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory |
|
40
|
|
|
* @param TaxonViewFactoryInterface $taxonViewFactory |
|
41
|
|
|
* @param string $fallback |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
ImageViewFactoryInterface $imageViewFactory, |
|
45
|
|
|
ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory, |
|
46
|
|
|
TaxonViewFactoryInterface $taxonViewFactory, |
|
47
|
|
|
$fallback |
|
48
|
|
|
) { |
|
49
|
|
|
$this->imageViewFactory = $imageViewFactory; |
|
50
|
|
|
$this->attributeValuesViewFactory = $attributeValuesViewFactory; |
|
51
|
|
|
$this->taxonViewFactory = $taxonViewFactory; |
|
52
|
|
|
$this->fallback = $fallback; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function create(ProductInterface $product, ChannelInterface $channel, string $locale): ProductView |
|
59
|
|
|
{ |
|
60
|
|
|
$productView = new ProductView(); |
|
61
|
|
|
$productView->code = $product->getCode(); |
|
62
|
|
|
$productView->averageRating = $product->getAverageRating(); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** @var ProductTranslationInterface $translation */ |
|
65
|
|
|
$translation = $product->getTranslation($locale); |
|
66
|
|
|
$productView->name = $translation->getName(); |
|
67
|
|
|
$productView->slug = $translation->getSlug(); |
|
68
|
|
|
|
|
69
|
|
|
/** @var ProductImageInterface $image */ |
|
70
|
|
|
foreach ($product->getImages() as $image) { |
|
71
|
|
|
$imageView = $this->imageViewFactory->create($image); |
|
72
|
|
|
$productView->images[] = $imageView; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @var TaxonInterface $taxon */ |
|
76
|
|
|
foreach ($product->getTaxons() as $taxon) { |
|
77
|
|
|
$productView->taxons[$taxon->getCode()] = $this->getTaxonWithAncestors($taxon, $locale); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$productView->attributes = $this->attributeValuesViewFactory->create($product->getAttributesByLocale($locale, $this->fallback)); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
return $productView; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function getTaxonWithAncestors(TaxonInterface $taxon, $locale): TaxonView |
|
86
|
|
|
{ |
|
87
|
|
|
$currentTaxonView = $this->taxonViewFactory->create($taxon, $locale); |
|
88
|
|
|
|
|
89
|
|
|
if (null === $taxon->getParent()) { |
|
90
|
|
|
return $currentTaxonView; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$taxonView = $this->getTaxonWithAncestors($taxon->getParent(), $locale); |
|
|
|
|
|
|
94
|
|
|
$taxonView->children[] = $currentTaxonView; |
|
95
|
|
|
|
|
96
|
|
|
return $taxonView; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.