Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageEntryConverter.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
21  const MEDIA_TYPE_CODE = 'image';
22 
27 
31  protected $dataObjectHelper;
32 
37  public function __construct(
38  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory,
39  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
40  ) {
41  $this->mediaGalleryEntryFactory = $mediaGalleryEntryFactory;
42  $this->dataObjectHelper = $dataObjectHelper;
43  }
44 
48  public function getMediaEntryType()
49  {
50  return self::MEDIA_TYPE_CODE;
51  }
52 
58  public function convertTo(Product $product, array $rowData)
59  {
60  $image = $rowData;
61  $productImages = $product->getMediaAttributeValues();
62  if (!isset($image['types'])) {
63  $image['types'] = array_keys($productImages, $image['file']);
64  }
65  $entry = $this->mediaGalleryEntryFactory->create();
66  $this->dataObjectHelper->populateWithArray(
67  $entry,
68  $image,
69  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
70  );
71  if (isset($image['value_id'])) {
72  $entry->setId($image['value_id']);
73  }
74  return $entry;
75  }
76 
82  {
83  $entryArray = [
84  'value_id' => $entry->getId(),
85  'file' => $entry->getFile(),
86  'label' => $entry->getLabel(),
87  'position' => $entry->getPosition(),
88  'disabled' => $entry->isDisabled(),
89  'types' => $entry->getTypes(),
90  'media_type' => $entry->getMediaType(),
91  'content' => $this->convertFromMediaGalleryEntryContentInterface($entry->getContent()),
92  ];
93  return $entryArray;
94  }
95 
102  ) {
103  if ($content === null) {
104  return null;
105  }
106 
107  return [
108  'data' => [
109  ImageContentInterface::BASE64_ENCODED_DATA => $content->getBase64EncodedData(),
112  ],
113  ];
114  }
115 }
$dataObjectHelper
getId()
__construct(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper)
getTypes()
getLabel()
getFile()
getMediaEntryType()
getContent()
convertFromMediaGalleryEntryContentInterface(ImageContentInterface $content=null)
convertFrom(ProductAttributeMediaGalleryEntryInterface $entry)
getPosition()
const MEDIA_TYPE_CODE
convertTo(Product $product, array $rowData)
isDisabled()
$mediaGalleryEntryFactory
getMediaType()