Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Copy.php
Go to the documentation of this file.
1 <?php
11 
12 class Copy
13 {
17  protected $fieldsetConfig;
18 
24  protected $eventManager = null;
25 
30 
36  public function __construct(
37  \Magento\Framework\Event\ManagerInterface $eventManager,
38  \Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
39  \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory
40  ) {
41  $this->eventManager = $eventManager;
42  $this->fieldsetConfig = $fieldsetConfig;
43  $this->extensionAttributesFactory = $extensionAttributesFactory;
44  }
45 
62  public function copyFieldsetToTarget($fieldset, $aspect, $source, $target, $root = 'global')
63  {
64  if (!$this->_isFieldsetInputValid($source, $target)) {
65  return null;
66  }
67  $fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
68  if ($fields === null) {
69  return $target;
70  }
71  $targetIsArray = is_array($target);
72 
73  foreach ($fields as $code => $node) {
74  if (empty($node[$aspect])) {
75  continue;
76  }
77 
79 
80  $targetCode = (string)$node[$aspect];
81  $targetCode = $targetCode == '*' ? $code : $targetCode;
82 
83  $target = $this->_setFieldsetFieldValue($target, $targetCode, $value);
84  }
85 
86  $target = $this->dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $target, $root, $targetIsArray);
87 
88  return $target;
89  }
90 
102  protected function dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $target, $root, $targetIsArray)
103  {
104  $eventName = sprintf('core_copy_fieldset_%s_%s', $fieldset, $aspect);
105  if ($targetIsArray) {
106  $target = new \Magento\Framework\DataObject($target);
107  }
108  $this->eventManager->dispatch(
109  $eventName,
110  ['target' => $target, 'source' => $source, 'root' => $root]
111  );
112  if ($targetIsArray) {
113  $target = $target->getData();
114  }
115  return $target;
116  }
117 
130  public function getDataFromFieldset($fieldset, $aspect, $source, $root = 'global')
131  {
132  if (!(is_array($source) || $source instanceof \Magento\Framework\DataObject)) {
133  return null;
134  }
135  $fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
136  if ($fields === null) {
137  return null;
138  }
139 
140  $data = [];
141  foreach ($fields as $code => $node) {
142  if (empty($node[$aspect])) {
143  continue;
144  }
145 
147 
148  $targetCode = (string)$node[$aspect];
149  $targetCode = $targetCode == '*' ? $code : $targetCode;
150  $data[$targetCode] = $value;
151  }
152 
153  return $data;
154  }
155 
163  protected function _isFieldsetInputValid($source, $target)
164  {
165  return (is_array($source) || $source instanceof \Magento\Framework\DataObject ||
166  $source instanceof \Magento\Framework\Api\ExtensibleDataInterface ||
167  $source instanceof \Magento\Framework\Api\AbstractSimpleObject) && (
168  is_array($target) || $target instanceof \Magento\Framework\DataObject ||
169  $target instanceof \Magento\Framework\Api\ExtensibleDataInterface ||
170  $target instanceof \Magento\Framework\Api\AbstractSimpleObject);
171  }
172 
182  protected function _getFieldsetFieldValue($source, $code)
183  {
184  if (is_array($source)) {
185  $value = isset($source[$code]) ? $source[$code] : null;
186  } elseif ($source instanceof \Magento\Framework\DataObject) {
187  $value = $source->getDataUsingMethod($code);
188  } elseif ($source instanceof \Magento\Framework\Api\ExtensibleDataInterface) {
190  } elseif ($source instanceof \Magento\Framework\Api\AbstractSimpleObject) {
191  $sourceArray = $source->__toArray();
192  $value = isset($sourceArray[$code]) ? $sourceArray[$code] : null;
193  } else {
194  throw new \InvalidArgumentException(
195  'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject'
196  );
197  }
198  return $value;
199  }
200 
211  protected function _setFieldsetFieldValue($target, $targetCode, $value)
212  {
213  $targetIsArray = is_array($target);
214 
215  if ($targetIsArray) {
216  $target[$targetCode] = $value;
217  } elseif ($target instanceof \Magento\Framework\DataObject) {
218  $target->setDataUsingMethod($targetCode, $value);
219  } elseif ($target instanceof \Magento\Framework\Api\ExtensibleDataInterface) {
221  } elseif ($target instanceof \Magento\Framework\Api\AbstractSimpleObject) {
222  $target->setData($targetCode, $value);
223  } else {
224  throw new \InvalidArgumentException(
225  'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject'
226  );
227  }
228 
229  return $target;
230  }
231 
242  {
243  $method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
244 
245  $methodExists = method_exists($source, $method);
246  if ($methodExists == true) {
247  $value = $source->{$method}();
248  } else {
249  // If we couldn't find the method, check if we can get it from the extension attributes
250  $extensionAttributes = $source->getExtensionAttributes();
251  if ($extensionAttributes == null) {
252  throw new \InvalidArgumentException('Method in extension does not exist.');
253  } else {
254  $extensionMethodExists = method_exists($extensionAttributes, $method);
255  if ($extensionMethodExists == true) {
257  } else {
258  throw new \InvalidArgumentException('Attribute in object does not exist.');
259  }
260  }
261  }
262  return $value;
263  }
264 
276  {
277  $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
278 
279  $methodExists = method_exists($target, $method);
280  if ($methodExists == true) {
281  $target->{$method}($value);
282  } else {
283  // If we couldn't find the method, check if we can set it from the extension attributes
284  $extensionAttributes = $target->getExtensionAttributes();
285  if ($extensionAttributes == null) {
286  $extensionAttributes = $this->extensionAttributesFactory->create(get_class($target));
287  }
288  $extensionMethodExists = method_exists($extensionAttributes, $method);
289  if ($extensionMethodExists == true) {
291  $target->setExtensionAttributes($extensionAttributes);
292  } else {
293  throw new \InvalidArgumentException('Attribute in object does not exist.');
294  }
295  }
296 
297  return null;
298  }
299 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
$fields
Definition: details.phtml:14
_isFieldsetInputValid($source, $target)
Definition: Copy.php:163
__construct(\Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\DataObject\Copy\Config $fieldsetConfig, \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory)
Definition: Copy.php:36
getAttributeValueFromExtensibleDataObject($source, $code)
Definition: Copy.php:241
$value
Definition: gender.phtml:16
setAttributeValueFromExtensibleDataObject($target, $code, $value)
Definition: Copy.php:275
$extensionAttributes
Definition: payment.php:22
$method
Definition: info.phtml:13
copyFieldsetToTarget($fieldset, $aspect, $source, $target, $root='global')
Definition: Copy.php:62
dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $target, $root, $targetIsArray)
Definition: Copy.php:102
_getFieldsetFieldValue($source, $code)
Definition: Copy.php:182
getDataFromFieldset($fieldset, $aspect, $source, $root='global')
Definition: Copy.php:130
_setFieldsetFieldValue($target, $targetCode, $value)
Definition: Copy.php:211
$code
Definition: info.phtml:12