Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TypeResolver.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  private $metadataPool;
17 
21  private $typeMapping = [
22  \Magento\SalesRule\Model\Rule::class => \Magento\SalesRule\Api\Data\RuleInterface::class,
23  \Magento\SalesRule\Model\Rule\Interceptor::class => \Magento\SalesRule\Api\Data\RuleInterface::class,
24  \Magento\SalesRule\Model\Rule\Proxy::class => \Magento\SalesRule\Api\Data\RuleInterface::class
25  ];
26 
31  public function __construct(MetadataPool $metadataPool)
32  {
33  $this->metadataPool = $metadataPool;
34  }
35 
41  public function resolve($type)
42  {
43  // @todo remove after MAGETWO-52608 resolved
44  $className = get_class($type);
45  if (isset($this->typeMapping[$className])) {
46  return $this->typeMapping[$className];
47  }
48 
49  $reflectionClass = new \ReflectionClass($type);
50  $interfaceNames = $reflectionClass->getInterfaceNames();
51  $dataInterfaces = [];
52  foreach ($interfaceNames as $interfaceName) {
53  if (strpos($interfaceName, '\Api\Data\\') !== false) {
54  $dataInterfaces[] = $interfaceName;
55  }
56  }
57 
58  if (count($dataInterfaces) == 0) {
59  return $className;
60  }
61 
62  foreach ($dataInterfaces as $dataInterface) {
63  if ($this->metadataPool->hasConfiguration($dataInterface)) {
64  $this->typeMapping[$className] = $dataInterface;
65  }
66  }
67  if (empty($this->typeMapping[$className])) {
68  $this->typeMapping[$className] = reset($dataInterfaces);
69  }
70  return $this->typeMapping[$className];
71  }
72 }
$type
Definition: item.phtml:13
$reflectionClass
Definition: categories.php:25
__construct(MetadataPool $metadataPool)
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31