Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataExtensionUtil.php
Go to the documentation of this file.
1 <?php
8 
13 
15 {
19  public function __construct()
20  {
21  // empty
22  }
23 
31  public function extendEntity($entityObject)
32  {
33  // Check to see if the parent entity exists
34  $parentEntity = DataObjectHandler::getInstance()->getObject($entityObject->getParentName());
35  if ($parentEntity == null) {
36  throw new XmlException(
37  "Parent Entity " .
38  $entityObject->getParentName() .
39  " not defined for Entity " .
40  $entityObject->getName() .
41  "." .
42  PHP_EOL
43  );
44  }
45 
46  // Check to see if the parent entity is already an extended entity
47  if ($parentEntity->getParentName() !== null) {
48  throw new XmlException(
49  "Cannot extend an entity that already extends another entity. Entity: " .
50  $parentEntity->getName() .
51  "." .
52  PHP_EOL
53  );
54  }
55  if (MftfApplicationConfig::getConfig()->verboseEnabled() &&
57  print("Extending Data: " . $parentEntity->getName() . " => " . $entityObject->getName() . PHP_EOL);
58  }
59 
60  //get parent entity type if child does not have a type
61  $newType = $entityObject->getType() ?? $parentEntity->getType();
62 
63  // Get all data for both parent and child and merge
64  $referencedData = $parentEntity->getAllData();
65  $newData = array_merge($referencedData, $entityObject->getAllData());
66 
67  // Get all linked references for both parent and child and merge
68  $referencedLinks = $parentEntity->getLinkedEntities();
69  $newLinkedReferences = array_merge($referencedLinks, $entityObject->getLinkedEntities());
70 
71  // Get all unique references for both parent and child and merge
72  $referencedUniqueData = $parentEntity->getUniquenessData();
73  $newUniqueReferences = array_merge($referencedUniqueData, $entityObject->getUniquenessData());
74 
75  // Get all var references for both parent and child and merge
76  $referencedVars = $parentEntity->getVarReferences();
77  $newVarReferences = array_merge($referencedVars, $entityObject->getVarReferences());
78 
79  // Remove unique references for objects that are replaced without such reference
80  $unmatchedUniqueReferences = array_diff_key($referencedUniqueData, $entityObject->getUniquenessData());
81  foreach ($unmatchedUniqueReferences as $uniqueKey => $uniqueData) {
82  if (array_key_exists($uniqueKey, $entityObject->getAllData())) {
83  unset($newUniqueReferences[$uniqueKey]);
84  }
85  }
86 
87  // Create new entity object to return
88  $extendedEntity = new EntityDataObject(
89  $entityObject->getName(),
90  $newType,
91  $newData,
92  $newLinkedReferences,
93  $newUniqueReferences,
94  $newVarReferences,
95  $entityObject->getParentName()
96  );
97  return $extendedEntity;
98  }
99 }