Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ItemConverter.php
Go to the documentation of this file.
1 <?php
7 
12 
19 {
23  private $configurationPool;
24 
28  private $eventManager;
29 
33  private $totalsItemFactory;
34 
38  private $dataObjectHelper;
39 
43  private $serializer;
44 
55  public function __construct(
56  ConfigurationPool $configurationPool,
57  EventManager $eventManager,
58  \Magento\Quote\Api\Data\TotalsItemInterfaceFactory $totalsItemFactory,
59  DataObjectHelper $dataObjectHelper,
60  \Magento\Framework\Serialize\Serializer\Json $serializer = null
61  ) {
62  $this->configurationPool = $configurationPool;
63  $this->eventManager = $eventManager;
64  $this->totalsItemFactory = $totalsItemFactory;
65  $this->dataObjectHelper = $dataObjectHelper;
66  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
67  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
68  }
69 
77  public function modelToDataObject($item)
78  {
79  $this->eventManager->dispatch('items_additional_data', ['item' => $item]);
80  $items = $item->toArray();
81  $items['options'] = $this->getFormattedOptionValue($item);
83 
84  $itemsData = $this->totalsItemFactory->create();
85  $this->dataObjectHelper->populateWithArray(
86  $itemsData,
87  $items,
88  \Magento\Quote\Api\Data\TotalsItemInterface::class
89  );
90  return $itemsData;
91  }
92 
99  private function getFormattedOptionValue($item)
100  {
101  $optionsData = [];
102 
103  /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
104  $helper = $this->configurationPool->getByProductType('default');
105 
106  $options = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
107  foreach ($options as $index => $optionValue) {
108  $params = [
109  'max_length' => 55,
110  'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
111  ];
112  $option = $helper->getFormattedOptionValue($optionValue, $params);
114  $optionsData[$index]['label'] = $optionValue['label'];
115  }
116  return $this->serializer->serialize($optionsData);
117  }
118 }
$helper
Definition: iframe.phtml:13
__construct(ConfigurationPool $configurationPool, EventManager $eventManager, \Magento\Quote\Api\Data\TotalsItemInterfaceFactory $totalsItemFactory, DataObjectHelper $dataObjectHelper, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44
$items