Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OperationElementExtractor.php
Go to the documentation of this file.
1 <?php
8 
11 
13 {
14  const OPERATION_OBJECT_KEY = 'key';
15  const OPERATION_OBJECT_DATA_TYPE = 'dataType';
16  const OPERATION_OBJECT_ARRAY = 'array';
17  const OPERATION_OBJECT_ENTRY = 'field';
18  const OPERATION_OBJECT_OBJ_NAME = 'object';
20 
24  public function __construct()
25  {
26  // public constructor
27  }
28 
36  public function extractOperationElement($operationElementArray)
37  {
38  // extract key
39  $operationDefKey = $operationElementArray[OperationElementExtractor::OPERATION_OBJECT_KEY];
40 
41  // extract dataType
42  $dataType = $operationElementArray[OperationElementExtractor::OPERATION_OBJECT_DATA_TYPE];
43 
44  $operationElements = [];
45  $nestedOperationElements = [];
46 
47  // extract nested entries
48  if (array_key_exists(OperationElementExtractor::OPERATION_OBJECT_ENTRY, $operationElementArray)) {
49  $this->extractOperationField(
50  $operationElements,
52  );
53  }
54 
55  // extract nested arrays
56  if (array_key_exists(OperationElementExtractor::OPERATION_OBJECT_ARRAY, $operationElementArray)) {
57  $this->extractOperationArray(
58  $operationElements,
60  );
61  }
62 
63  // extract nested
64  if (array_key_exists(OperationElementExtractor::OPERATION_OBJECT_OBJ_NAME, $operationElementArray)) {
65  foreach ($operationElementArray[OperationElementExtractor::OPERATION_OBJECT_OBJ_NAME] as $operationObject) {
66  $nestedOperationElement = $this->extractOperationElement($operationObject);
67  $operationElements[] = $nestedOperationElement;
68  }
69  }
70 
71  // a dataObject specified in xml must contain corresponding metadata for the object
72  if (empty($operationElements)) {
73  throw new \Exception("must specify dataObject metadata if declaration is used");
74  }
75 
76  return new OperationElement(
77  $operationDefKey,
78  $dataType,
81  $nestedOperationElements,
82  $operationElements
83  );
84  }
85 
93  private function extractOperationField(&$operationElements, $operationFieldArray)
94  {
95  foreach ($operationFieldArray as $operationFieldType) {
96  $operationElements[] = new OperationElement(
101  );
102  }
103  }
104 
112  private function extractOperationArray(&$operationArrayData, $operationArrayArray)
113  {
114  foreach ($operationArrayArray as $operationFieldType) {
115  $operationElementValue =
118 
119  $nestedOperationElements = [];
120  if (array_key_exists(OperationElementExtractor::OPERATION_OBJECT_OBJ_NAME, $operationFieldType)) {
121  //add the key to reference this object later
122  $operationObjectKeyedArray = $operationFieldType
124  $operationObjectKeyedArray[OperationElementExtractor::OPERATION_OBJECT_KEY] =
126  $operationElement = $this->extractOperationElement($operationObjectKeyedArray);
127  $operationElementValue = $operationElement->getValue();
128  $nestedOperationElements[$operationElement->getValue()] = $operationElement;
129  }
130  $operationArrayData[] = new OperationElement(
132  $operationElementValue,
135  $nestedOperationElements
136  );
137  }
138  }
139 }