SimpleDataObjectConverter.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api;
  7. use Magento\Framework\Convert\ConvertArray;
  8. use Magento\Framework\Reflection\DataObjectProcessor;
  9. /**
  10. * Data object converter.
  11. */
  12. class SimpleDataObjectConverter
  13. {
  14. /**
  15. * @var DataObjectProcessor
  16. */
  17. protected $dataObjectProcessor;
  18. /**
  19. * @param DataObjectProcessor $dataObjectProcessor
  20. */
  21. public function __construct(DataObjectProcessor $dataObjectProcessor)
  22. {
  23. $this->dataObjectProcessor = $dataObjectProcessor;
  24. }
  25. /**
  26. * Convert nested array into flat array.
  27. *
  28. * @param ExtensibleDataInterface $dataObject
  29. * @param string $dataObjectType
  30. * @return array
  31. */
  32. public function toFlatArray(ExtensibleDataInterface $dataObject, $dataObjectType = null)
  33. {
  34. if ($dataObjectType === null) {
  35. $dataObjectType = get_class($dataObject);
  36. }
  37. $data = $this->dataObjectProcessor->buildOutputDataArray($dataObject, $dataObjectType);
  38. return ConvertArray::toFlatArray($data);
  39. }
  40. /**
  41. * Convert keys to camelCase
  42. *
  43. * @param array $dataArray
  44. * @return \stdClass
  45. */
  46. public function convertKeysToCamelCase(array $dataArray)
  47. {
  48. $response = [];
  49. if (isset($dataArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY])) {
  50. $dataArray = ExtensibleDataObjectConverter::convertCustomAttributesToSequentialArray($dataArray);
  51. }
  52. foreach ($dataArray as $fieldName => $fieldValue) {
  53. if (is_array($fieldValue) && !$this->_isSimpleSequentialArray($fieldValue)) {
  54. $fieldValue = $this->convertKeysToCamelCase($fieldValue);
  55. }
  56. $fieldName = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $fieldName))));
  57. $response[$fieldName] = $fieldValue;
  58. }
  59. return $response;
  60. }
  61. /**
  62. * Check if the array is a simple(one dimensional and not nested) and a sequential(non-associative) array
  63. *
  64. * @param array $data
  65. * @return bool
  66. */
  67. protected function _isSimpleSequentialArray(array $data)
  68. {
  69. foreach ($data as $key => $value) {
  70. if (is_string($key) || is_array($value)) {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. /**
  77. * Convert multidimensional object/array into multidimensional array of primitives.
  78. *
  79. * @param object|array $input
  80. * @param bool $removeItemNode Remove Item node from arrays if true
  81. * @return array
  82. * @throws \InvalidArgumentException
  83. * @SuppressWarnings(PHPMD.NPathComplexity)
  84. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  85. */
  86. public function convertStdObjectToArray($input, $removeItemNode = false)
  87. {
  88. if (!is_object($input) && !is_array($input)) {
  89. throw new \InvalidArgumentException("Input argument must be an array or object");
  90. }
  91. // @codingStandardsIgnoreStart
  92. if ($removeItemNode && (isset($input->item) || isset($input->Map))) {
  93. $node = isset($input->item) ? $input->item : $input->Map;
  94. /**
  95. * In case when only one Data object value is passed, it will not be wrapped into a subarray
  96. * within any additional node. If several Data object values are passed, they will be wrapped into
  97. * an indexed array within item or Map node.
  98. */
  99. $input = is_object($node) ? [$node] : $node;
  100. }
  101. // @codingStandardsIgnoreEnd
  102. $result = [];
  103. foreach ((array)$input as $key => $value) {
  104. if (is_object($value) || is_array($value)) {
  105. $result[$key] = $this->convertStdObjectToArray($value, $removeItemNode);
  106. } else {
  107. $result[$key] = $value;
  108. }
  109. }
  110. return $this->_unpackAssociativeArray($result);
  111. }
  112. /**
  113. * Unpack associative array packed by SOAP server into key-value
  114. *
  115. * @param mixed $data
  116. * @return array Unpacked associative array if array was passed as argument or original value otherwise
  117. */
  118. protected function _unpackAssociativeArray($data)
  119. {
  120. if (!is_array($data)) {
  121. return $data;
  122. } else {
  123. foreach ($data as $key => $value) {
  124. if (is_array($value) && count($value) == 2 && isset($value['key']) && isset($value['value'])) {
  125. $data[$value['key']] = $this->_unpackAssociativeArray($value['value']);
  126. unset($data[$key]);
  127. } else {
  128. $data[$key] = $this->_unpackAssociativeArray($value);
  129. }
  130. }
  131. return $data;
  132. }
  133. }
  134. /**
  135. * Converts an input string from snake_case to upper CamelCase.
  136. *
  137. * @param string $input
  138. * @return string
  139. */
  140. public static function snakeCaseToUpperCamelCase($input)
  141. {
  142. return str_replace(' ', '', ucwords(str_replace('_', ' ', $input)));
  143. }
  144. /**
  145. * Converts an input string from snake_case to camelCase.
  146. *
  147. * @param string $input
  148. * @return string
  149. */
  150. public static function snakeCaseToCamelCase($input)
  151. {
  152. return lcfirst(self::snakeCaseToUpperCamelCase($input));
  153. }
  154. /**
  155. * Convert a CamelCase string read from method into field key in snake_case
  156. *
  157. * For example [DefaultShipping => default_shipping, Postcode => postcode]
  158. *
  159. * @param string $name
  160. * @return string
  161. */
  162. public static function camelCaseToSnakeCase($name)
  163. {
  164. return strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
  165. }
  166. }