Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataDifferenceCalculator.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
21  private $runtimeConfigSource;
22 
26  public function __construct(ConfigSourceInterface $runtimeConfigSource)
27  {
28  $this->runtimeConfigSource = $runtimeConfigSource;
29  }
30 
38  public function getItemsToDelete($scope, array $data)
39  {
40  $data = $this->changeDataKeyToCode($data);
41  $runtimeScopeData = $this->changeDataKeyToCode(
42  $this->getRuntimeData($scope)
43  );
44 
45  return array_diff_key($runtimeScopeData, $data);
46  }
47 
55  public function getItemsToCreate($scope, array $data)
56  {
57  $data = $this->changeDataKeyToCode($data);
58  $runtimeScopeData = $this->changeDataKeyToCode(
59  $this->getRuntimeData($scope)
60  );
61 
62  return array_diff_key($data, $runtimeScopeData);
63  }
64 
72  public function getItemsToUpdate($scope, array $data)
73  {
74  $itemsToUpdate = [];
75  $data = $this->changeDataKeyToCode($data);
76  $data = $this->setDefaultValues($scope, $data);
77  $runtimeScopeData = $this->changeDataKeyToCode(
78  $this->getRuntimeData($scope)
79  );
80 
81  foreach ($runtimeScopeData as $entityCode => $entityData) {
82  if (isset($data[$entityCode]) && array_diff_assoc($entityData, $data[$entityCode])) {
83  $itemsToUpdate[$entityCode] = array_replace($entityData, $data[$entityCode]);
84  }
85  }
86 
87  return $itemsToUpdate;
88  }
89 
97  private function setDefaultValues($scope, array $data)
98  {
99  $fieldset = [];
100  switch ($scope) {
102  $fieldset = ['default_group_id'];
103  break;
105  $fieldset = ['website_id', 'default_store_id', 'root_category_id'];
106  break;
108  $fieldset = ['website_id', 'group_id'];
109  break;
110  }
111 
112  foreach ($data as $entityCode => $entityData) {
113  foreach ($fieldset as $field) {
114  $entityData[$field] = !empty($entityData[$field]) ? $entityData[$field] : '0';
115  }
116 
117  $data[$entityCode] = $entityData;
118  }
119 
120  return $data;
121  }
122 
129  private function getRuntimeData($scope)
130  {
131  $runtimeData = $this->runtimeConfigSource->get();
132 
133  return (array)$runtimeData[$scope];
134  }
135 
142  private function changeDataKeyToCode(array $data)
143  {
144  return array_combine(
145  array_column($data, 'code'),
146  array_values($data)
147  );
148  }
149 }