Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeserializerFactory.php
Go to the documentation of this file.
1 <?php
9 
11 
13 {
17  protected $_objectManager;
18 
22  protected $_deserializers;
23 
28  public function __construct(
30  array $deserializers = []
31  ) {
32  $this->_objectManager = $objectManager;
33  $this->_deserializers = $deserializers;
34  }
35 
43  public function get($contentType)
44  {
45  if (empty($this->_deserializers)) {
46  throw new \LogicException('Request deserializer adapter is not set.');
47  }
48  foreach ($this->_deserializers as $deserializerMetadata) {
49  $deserializerType = $deserializerMetadata['type'];
50  if ($deserializerType == $contentType) {
51  $deserializerClass = $deserializerMetadata['model'];
52  break;
53  }
54  }
55 
56  if (!isset($deserializerClass) || empty($deserializerClass)) {
57  throw new \Magento\Framework\Webapi\Exception(
58  new Phrase('Server cannot understand Content-Type HTTP header media type %1', [$contentType])
59  );
60  }
61 
62  $deserializer = $this->_objectManager->get($deserializerClass);
63  if (!$deserializer instanceof \Magento\Framework\Webapi\Rest\Request\DeserializerInterface) {
64  throw new \LogicException(
65  'The deserializer must implement "Magento\Framework\Webapi\Rest\Request\DeserializerInterface".'
66  );
67  }
68  return $deserializer;
69  }
70 }
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, array $deserializers=[])