Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ComplexType.php
Go to the documentation of this file.
1 <?php
8 
15 
20 {
24  private $stringUtility;
25 
29  private $attributeRepository;
30 
34  private $backendModelToAttributeTypeMap;
35 
44  public function __construct(
45  StringUtils $stringUtility,
46  AttributeRepositoryInterface $attributeRepository,
47  array $backendModelToAttributeTypeMap
48  ) {
49  $this->stringUtility = $stringUtility;
50  $this->attributeRepository = $attributeRepository;
51  $this->backendModelToAttributeTypeMap = $backendModelToAttributeTypeMap;
52  }
53 
58  {
59  try {
60  $attribute = $this->attributeRepository->get($entityType, $attributeCode);
61  } catch (NoSuchEntityException $exception) {
63  }
64  $backendModel = $attribute->getBackendModel();
65  $attributeTypeMap = $this->getAttributeBackendModelToTypeMapping();
66  //If empty backend model, check if it can be derived
67  if (empty($backendModel)
68  && ($attribute->getBackendType() == 'static'
69  || $attribute->getFrontendInput() === 'image')
70  ) {
71  $backendModelClass = sprintf(
72  'Magento\Eav\Model\Attribute\Data\%s',
73  $this->stringUtility->upperCaseWords($attribute->getFrontendInput())
74  );
75  $backendModel = class_exists($backendModelClass) ? $backendModelClass : null;
76  }
77 
78  $dataInterface = isset($attributeTypeMap[$backendModel])
79  ? $attributeTypeMap[$backendModel]
81 
82  return $dataInterface;
83  }
84 
88  public function getDataTypes()
89  {
90  $dataInterfaceArray = [];
91  foreach ($this->getAttributeBackendModelToTypeMapping() as $attributeType) {
92  if (interface_exists($attributeType)) {
93  $dataInterfaceArray[] = $attributeType;
94  }
95  }
96  return array_unique($dataInterfaceArray);
97  }
98 
102  public function getAllServiceDataInterfaces()
103  {
104  return $this->getDataTypes();
105  }
106 
110  private function getAttributeBackendModelToTypeMapping()
111  {
112  $attributeTypeMap = [];
113  foreach ($this->backendModelToAttributeTypeMap as $key => $value) {
114  if (is_array($value)) {
115  // Alternative declaration format is supported for backward compatibility
116  foreach ($value as $backendModel => $attributeType) {
117  $attributeTypeMap[$backendModel] = $attributeType;
118  }
119  } else {
120  $attributeTypeMap[$key] = $value;
121  }
122  }
123  return $attributeTypeMap;
124  }
125 }
getType($attributeCode, $entityType)
Definition: ComplexType.php:57
__construct(StringUtils $stringUtility, AttributeRepositoryInterface $attributeRepository, array $backendModelToAttributeTypeMap)
Definition: ComplexType.php:44
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12