Copy.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DataObject;
  7. /**
  8. * Utility class for copying data sets between objects
  9. */
  10. class Copy
  11. {
  12. /**
  13. * @var \Magento\Framework\DataObject\Copy\Config
  14. */
  15. protected $fieldsetConfig;
  16. /**
  17. * Core event manager proxy
  18. *
  19. * @var \Magento\Framework\Event\ManagerInterface
  20. */
  21. protected $eventManager = null;
  22. /**
  23. * @var \Magento\Framework\Api\ExtensionAttributesFactory
  24. */
  25. protected $extensionAttributesFactory;
  26. /**
  27. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  28. * @param \Magento\Framework\DataObject\Copy\Config $fieldsetConfig
  29. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory
  30. */
  31. public function __construct(
  32. \Magento\Framework\Event\ManagerInterface $eventManager,
  33. \Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
  34. \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory
  35. ) {
  36. $this->eventManager = $eventManager;
  37. $this->fieldsetConfig = $fieldsetConfig;
  38. $this->extensionAttributesFactory = $extensionAttributesFactory;
  39. }
  40. /**
  41. * Copy data from object|array to object|array containing fields from fieldset matching an aspect.
  42. *
  43. * Contents of $aspect are a field name in target object or array.
  44. * If targetField attribute is not provided - will be used the same name as in the source object or array.
  45. *
  46. * @param string $fieldset
  47. * @param string $aspect
  48. * @param array|\Magento\Framework\DataObject $source
  49. * @param array|\Magento\Framework\DataObject $target
  50. * @param string $root
  51. * @return array|\Magento\Framework\DataObject|null the value of $target
  52. * @throws \InvalidArgumentException
  53. *
  54. * @api
  55. */
  56. public function copyFieldsetToTarget($fieldset, $aspect, $source, $target, $root = 'global')
  57. {
  58. if (!$this->_isFieldsetInputValid($source, $target)) {
  59. return null;
  60. }
  61. $fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
  62. if ($fields === null) {
  63. return $target;
  64. }
  65. $targetIsArray = is_array($target);
  66. foreach ($fields as $code => $node) {
  67. if (empty($node[$aspect])) {
  68. continue;
  69. }
  70. $value = $this->_getFieldsetFieldValue($source, $code);
  71. $targetCode = (string)$node[$aspect];
  72. $targetCode = $targetCode == '*' ? $code : $targetCode;
  73. $target = $this->_setFieldsetFieldValue($target, $targetCode, $value);
  74. }
  75. $target = $this->dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $target, $root, $targetIsArray);
  76. return $target;
  77. }
  78. /**
  79. * Dispatch copy fieldset event
  80. *
  81. * @param string $fieldset
  82. * @param string $aspect
  83. * @param array|\Magento\Framework\DataObject $source
  84. * @param array|\Magento\Framework\DataObject $target
  85. * @param string $root
  86. * @param bool $targetIsArray
  87. * @return \Magento\Framework\DataObject|mixed
  88. */
  89. protected function dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $target, $root, $targetIsArray)
  90. {
  91. $eventName = sprintf('core_copy_fieldset_%s_%s', $fieldset, $aspect);
  92. if ($targetIsArray) {
  93. $target = new \Magento\Framework\DataObject($target);
  94. }
  95. $this->eventManager->dispatch(
  96. $eventName,
  97. ['target' => $target, 'source' => $source, 'root' => $root]
  98. );
  99. if ($targetIsArray) {
  100. $target = $target->getData();
  101. }
  102. return $target;
  103. }
  104. /**
  105. * Get data from object|array to object|array containing fields from fieldset matching an aspect.
  106. *
  107. * @param string $fieldset
  108. * @param string $aspect a field name
  109. * @param array|\Magento\Framework\DataObject $source
  110. * @param string $root
  111. * @return array
  112. *
  113. * @api
  114. */
  115. public function getDataFromFieldset($fieldset, $aspect, $source, $root = 'global')
  116. {
  117. if (!(is_array($source) || $source instanceof \Magento\Framework\DataObject)) {
  118. return null;
  119. }
  120. $fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
  121. if ($fields === null) {
  122. return null;
  123. }
  124. $data = [];
  125. foreach ($fields as $code => $node) {
  126. if (empty($node[$aspect])) {
  127. continue;
  128. }
  129. $value = $this->_getFieldsetFieldValue($source, $code);
  130. $targetCode = (string)$node[$aspect];
  131. $targetCode = $targetCode == '*' ? $code : $targetCode;
  132. $data[$targetCode] = $value;
  133. }
  134. return $data;
  135. }
  136. /**
  137. * Check if source and target are valid input for converting using fieldset
  138. *
  139. * @param array|\Magento\Framework\DataObject $source
  140. * @param array|\Magento\Framework\DataObject $target
  141. * @return bool
  142. */
  143. protected function _isFieldsetInputValid($source, $target)
  144. {
  145. return (is_array($source) || $source instanceof \Magento\Framework\DataObject ||
  146. $source instanceof \Magento\Framework\Api\ExtensibleDataInterface ||
  147. $source instanceof \Magento\Framework\Api\AbstractSimpleObject) && (
  148. is_array($target) || $target instanceof \Magento\Framework\DataObject ||
  149. $target instanceof \Magento\Framework\Api\ExtensibleDataInterface ||
  150. $target instanceof \Magento\Framework\Api\AbstractSimpleObject);
  151. }
  152. /**
  153. * Get value of source by code
  154. *
  155. * @param mixed $source
  156. * @param string $code
  157. *
  158. * @return mixed
  159. * @throws \InvalidArgumentException
  160. */
  161. protected function _getFieldsetFieldValue($source, $code)
  162. {
  163. if (is_array($source)) {
  164. $value = isset($source[$code]) ? $source[$code] : null;
  165. } elseif ($source instanceof \Magento\Framework\DataObject) {
  166. $value = $source->getDataUsingMethod($code);
  167. } elseif ($source instanceof \Magento\Framework\Api\ExtensibleDataInterface) {
  168. $value = $this->getAttributeValueFromExtensibleDataObject($source, $code);
  169. } elseif ($source instanceof \Magento\Framework\Api\AbstractSimpleObject) {
  170. $sourceArray = $source->__toArray();
  171. $value = isset($sourceArray[$code]) ? $sourceArray[$code] : null;
  172. } else {
  173. throw new \InvalidArgumentException(
  174. 'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject'
  175. );
  176. }
  177. return $value;
  178. }
  179. /**
  180. * Set value of target by code
  181. *
  182. * @param mixed $target
  183. * @param string $targetCode
  184. * @param mixed $value
  185. *
  186. * @return mixed
  187. * @throws \InvalidArgumentException
  188. */
  189. protected function _setFieldsetFieldValue($target, $targetCode, $value)
  190. {
  191. $targetIsArray = is_array($target);
  192. if ($targetIsArray) {
  193. $target[$targetCode] = $value;
  194. } elseif ($target instanceof \Magento\Framework\DataObject) {
  195. $target->setDataUsingMethod($targetCode, $value);
  196. } elseif ($target instanceof \Magento\Framework\Api\ExtensibleDataInterface) {
  197. $this->setAttributeValueFromExtensibleDataObject($target, $targetCode, $value);
  198. } elseif ($target instanceof \Magento\Framework\Api\AbstractSimpleObject) {
  199. $target->setData($targetCode, $value);
  200. } else {
  201. throw new \InvalidArgumentException(
  202. 'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject'
  203. );
  204. }
  205. return $target;
  206. }
  207. /**
  208. * Access the extension get method
  209. *
  210. * @param \Magento\Framework\Api\ExtensibleDataInterface $source
  211. * @param string $code
  212. *
  213. * @return mixed
  214. * @throws \InvalidArgumentException
  215. */
  216. protected function getAttributeValueFromExtensibleDataObject($source, $code)
  217. {
  218. $method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
  219. $methodExists = method_exists($source, $method);
  220. if ($methodExists == true) {
  221. $value = $source->{$method}();
  222. } else {
  223. // If we couldn't find the method, check if we can get it from the extension attributes
  224. $extensionAttributes = $source->getExtensionAttributes();
  225. if ($extensionAttributes == null) {
  226. throw new \InvalidArgumentException('Method in extension does not exist.');
  227. } else {
  228. $extensionMethodExists = method_exists($extensionAttributes, $method);
  229. if ($extensionMethodExists == true) {
  230. $value = $extensionAttributes->{$method}();
  231. } else {
  232. throw new \InvalidArgumentException('Attribute in object does not exist.');
  233. }
  234. }
  235. }
  236. return $value;
  237. }
  238. /**
  239. * Access the extension set method
  240. *
  241. * @param \Magento\Framework\Api\ExtensibleDataInterface $target
  242. * @param string $code
  243. * @param mixed $value
  244. *
  245. * @return null
  246. * @throws \InvalidArgumentException
  247. */
  248. protected function setAttributeValueFromExtensibleDataObject($target, $code, $value)
  249. {
  250. $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
  251. $methodExists = method_exists($target, $method);
  252. if ($methodExists == true) {
  253. $target->{$method}($value);
  254. } else {
  255. // If we couldn't find the method, check if we can set it from the extension attributes
  256. $extensionAttributes = $target->getExtensionAttributes();
  257. if ($extensionAttributes == null) {
  258. $extensionAttributes = $this->extensionAttributesFactory->create(get_class($target));
  259. }
  260. $extensionMethodExists = method_exists($extensionAttributes, $method);
  261. if ($extensionMethodExists == true) {
  262. $extensionAttributes->{$method}($value);
  263. $target->setExtensionAttributes($extensionAttributes);
  264. } else {
  265. throw new \InvalidArgumentException('Attribute in object does not exist.');
  266. }
  267. }
  268. return null;
  269. }
  270. }