Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Copy Class Reference

Public Member Functions

 __construct (\Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\DataObject\Copy\Config $fieldsetConfig, \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory)
 
 copyFieldsetToTarget ($fieldset, $aspect, $source, $target, $root='global')
 
 getDataFromFieldset ($fieldset, $aspect, $source, $root='global')
 

Protected Member Functions

 dispatchCopyFieldSetEvent ($fieldset, $aspect, $source, $target, $root, $targetIsArray)
 
 _isFieldsetInputValid ($source, $target)
 
 _getFieldsetFieldValue ($source, $code)
 
 _setFieldsetFieldValue ($target, $targetCode, $value)
 
 getAttributeValueFromExtensibleDataObject ($source, $code)
 
 setAttributeValueFromExtensibleDataObject ($target, $code, $value)
 

Protected Attributes

 $fieldsetConfig
 
 $eventManager = null
 
 $extensionAttributesFactory
 

Detailed Description

Definition at line 12 of file Copy.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Event\ManagerInterface  $eventManager,
\Magento\Framework\DataObject\Copy\Config  $fieldsetConfig,
\Magento\Framework\Api\ExtensionAttributesFactory  $extensionAttributesFactory 
)
Parameters
\Magento\Framework\Event\ManagerInterface$eventManager
\Magento\Framework\DataObject\Copy\Config$fieldsetConfig
\Magento\Framework\Api\ExtensionAttributesFactory$extensionAttributesFactory

Definition at line 36 of file Copy.php.

40  {
41  $this->eventManager = $eventManager;
42  $this->fieldsetConfig = $fieldsetConfig;
43  $this->extensionAttributesFactory = $extensionAttributesFactory;
44  }

Member Function Documentation

◆ _getFieldsetFieldValue()

_getFieldsetFieldValue (   $source,
  $code 
)
protected

Get value of source by code

Parameters
mixed$source
string$code
Returns
mixed
Exceptions

Definition at line 182 of file Copy.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
getAttributeValueFromExtensibleDataObject($source, $code)
Definition: Copy.php:241
$value
Definition: gender.phtml:16
$code
Definition: info.phtml:12

◆ _isFieldsetInputValid()

_isFieldsetInputValid (   $source,
  $target 
)
protected

Check if source and target are valid input for converting using fieldset

Parameters
array | \Magento\Framework\DataObject$source
array | \Magento\Framework\DataObject$target
Returns
bool

Definition at line 163 of file Copy.php.

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  }
$source
Definition: source.php:23
$target
Definition: skip.phtml:8

◆ _setFieldsetFieldValue()

_setFieldsetFieldValue (   $target,
  $targetCode,
  $value 
)
protected

Set value of target by code

Parameters
mixed$target
string$targetCode
mixed$value
Returns
mixed
Exceptions

Definition at line 211 of file Copy.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$target
Definition: skip.phtml:8
$value
Definition: gender.phtml:16
setAttributeValueFromExtensibleDataObject($target, $code, $value)
Definition: Copy.php:275

◆ copyFieldsetToTarget()

copyFieldsetToTarget (   $fieldset,
  $aspect,
  $source,
  $target,
  $root = 'global' 
)

Copy data from object|array to object|array containing fields from fieldset matching an aspect.

Contents of $aspect are a field name in target object or array. If targetField attribute is not provided - will be used the same name as in the source object or array.

Parameters
string$fieldset
string$aspect
array | \Magento\Framework\DataObject$source
array | \Magento\Framework\DataObject$target
string$root
Returns
array|\Magento\Framework\DataObject|null the value of $target
Exceptions

Definition at line 62 of file Copy.php.

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  }
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
$fields
Definition: details.phtml:14
_isFieldsetInputValid($source, $target)
Definition: Copy.php:163
$value
Definition: gender.phtml:16
dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $target, $root, $targetIsArray)
Definition: Copy.php:102
_getFieldsetFieldValue($source, $code)
Definition: Copy.php:182
_setFieldsetFieldValue($target, $targetCode, $value)
Definition: Copy.php:211
$code
Definition: info.phtml:12

◆ dispatchCopyFieldSetEvent()

dispatchCopyFieldSetEvent (   $fieldset,
  $aspect,
  $source,
  $target,
  $root,
  $targetIsArray 
)
protected

Dispatch copy fieldset event

Parameters
string$fieldset
string$aspect
array | \Magento\Framework\DataObject$source
array | \Magento\Framework\DataObject$target
string$root
bool$targetIsArray
Returns
\Magento\Framework\DataObject|mixed

Definition at line 102 of file Copy.php.

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  }
$source
Definition: source.php:23
$target
Definition: skip.phtml:8

◆ getAttributeValueFromExtensibleDataObject()

getAttributeValueFromExtensibleDataObject (   $source,
  $code 
)
protected

Access the extension get method

Parameters
\Magento\Framework\Api\ExtensibleDataInterface$object
string$code
Returns
mixed
Exceptions

Definition at line 241 of file Copy.php.

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  }
$source
Definition: source.php:23
$value
Definition: gender.phtml:16
$extensionAttributes
Definition: payment.php:22
$method
Definition: info.phtml:13
$code
Definition: info.phtml:12

◆ getDataFromFieldset()

getDataFromFieldset (   $fieldset,
  $aspect,
  $source,
  $root = 'global' 
)

Get data from object|array to object|array containing fields from fieldset matching an aspect.

Parameters
string$fieldset
string$aspecta field name
array | \Magento\Framework\DataObject$source
string$root
Returns
array $data

@api

Definition at line 130 of file Copy.php.

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  }
$source
Definition: source.php:23
$fields
Definition: details.phtml:14
$value
Definition: gender.phtml:16
_getFieldsetFieldValue($source, $code)
Definition: Copy.php:182
$code
Definition: info.phtml:12

◆ setAttributeValueFromExtensibleDataObject()

setAttributeValueFromExtensibleDataObject (   $target,
  $code,
  $value 
)
protected

Access the extension set method

Parameters
\Magento\Framework\Api\ExtensibleDataInterface$object
string$code
mixed$value
Returns
null
Exceptions

Definition at line 275 of file Copy.php.

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  }
$target
Definition: skip.phtml:8
$value
Definition: gender.phtml:16
$extensionAttributes
Definition: payment.php:22
$method
Definition: info.phtml:13
$code
Definition: info.phtml:12

Field Documentation

◆ $eventManager

$eventManager = null
protected

Definition at line 24 of file Copy.php.

◆ $extensionAttributesFactory

$extensionAttributesFactory
protected

Definition at line 29 of file Copy.php.

◆ $fieldsetConfig

$fieldsetConfig
protected

Definition at line 17 of file Copy.php.


The documentation for this class was generated from the following file: