| @@ 13-51 (lines=39) @@ | ||
| 10 | use Sylius\ShopApiPlugin\View\ProductVariantView; |
|
| 11 | use Sylius\ShopApiPlugin\View\ProductView; |
|
| 12 | ||
| 13 | final class ProductViewFactory implements ProductViewFactoryInterface |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var ImageViewFactoryInterface |
|
| 17 | */ |
|
| 18 | private $imageViewFactory; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * @param ImageViewFactoryInterface $imageViewFactory |
|
| 22 | */ |
|
| 23 | public function __construct(ImageViewFactoryInterface $imageViewFactory) |
|
| 24 | { |
|
| 25 | $this->imageViewFactory = $imageViewFactory; |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * {@inheritdoc} |
|
| 30 | */ |
|
| 31 | public function create(ProductInterface $product, ChannelInterface $channel, $locale) |
|
| 32 | { |
|
| 33 | $productView = new ProductView(); |
|
| 34 | $productView->name = $product->getTranslation($locale)->getName(); |
|
| 35 | $productView->code = $product->getCode(); |
|
| 36 | $productView->slug = $product->getTranslation($locale)->getSlug(); |
|
| 37 | ||
| 38 | /** @var ProductImageInterface $image */ |
|
| 39 | foreach ($product->getImages() as $image) { |
|
| 40 | $imageView = $this->imageViewFactory->create($image); |
|
| 41 | $productView->images[] = $imageView; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** @var TaxonInterface $taxon */ |
|
| 45 | foreach ($product->getTaxons() as $taxon) { |
|
| 46 | $productView->taxons[] = $taxon->getCode(); |
|
| 47 | } |
|
| 48 | ||
| 49 | return $productView; |
|
| 50 | } |
|
| 51 | } |
|
| 52 | ||
| @@ 9-43 (lines=35) @@ | ||
| 6 | use Sylius\Component\Core\Model\TaxonInterface; |
|
| 7 | use Sylius\ShopApiPlugin\View\TaxonView; |
|
| 8 | ||
| 9 | final class TaxonViewFactory implements TaxonViewFactoryInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var ImageViewFactoryInterface |
|
| 13 | */ |
|
| 14 | private $imageViewFactory; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @param ImageViewFactoryInterface $imageViewFactory |
|
| 18 | */ |
|
| 19 | public function __construct(ImageViewFactoryInterface $imageViewFactory) |
|
| 20 | { |
|
| 21 | $this->imageViewFactory = $imageViewFactory; |
|
| 22 | } |
|
| 23 | ||
| 24 | /** |
|
| 25 | * {@inheritdoc} |
|
| 26 | */ |
|
| 27 | public function create(TaxonInterface $taxon, $locale) |
|
| 28 | { |
|
| 29 | $taxonView = new TaxonView(); |
|
| 30 | $taxonView->name = $taxon->getTranslation($locale)->getName(); |
|
| 31 | $taxonView->code = $taxon->getCode(); |
|
| 32 | $taxonView->slug = $taxon->getTranslation($locale)->getSlug(); |
|
| 33 | $taxonView->description = $taxon->getTranslation($locale)->getDescription(); |
|
| 34 | $taxonView->position = $taxon->getPosition(); |
|
| 35 | ||
| 36 | /** @var ImageInterface $image */ |
|
| 37 | foreach ($taxon->getImages() as $image) { |
|
| 38 | $taxonView->images[] = $this->imageViewFactory->create($image); |
|
| 39 | } |
|
| 40 | ||
| 41 | return $taxonView; |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||