Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SimpleDataObjectConverter.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\Api;
7 
10 
12 {
17 
22  {
23  $this->dataObjectProcessor = $dataObjectProcessor;
24  }
25 
33  public function toFlatArray(ExtensibleDataInterface $dataObject, $dataObjectType = null)
34  {
35  if ($dataObjectType === null) {
36  $dataObjectType = get_class($dataObject);
37  }
38  $data = $this->dataObjectProcessor->buildOutputDataArray($dataObject, $dataObjectType);
40  }
41 
48  public function convertKeysToCamelCase(array $dataArray)
49  {
50  $response = [];
51  if (isset($dataArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY])) {
53  }
54  foreach ($dataArray as $fieldName => $fieldValue) {
55  if (is_array($fieldValue) && !$this->_isSimpleSequentialArray($fieldValue)) {
56  $fieldValue = $this->convertKeysToCamelCase($fieldValue);
57  }
58  $fieldName = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $fieldName))));
59  $response[$fieldName] = $fieldValue;
60  }
61  return $response;
62  }
63 
70  protected function _isSimpleSequentialArray(array $data)
71  {
72  foreach ($data as $key => $value) {
73  if (is_string($key) || is_array($value)) {
74  return false;
75  }
76  }
77  return true;
78  }
79 
90  public function convertStdObjectToArray($input, $removeItemNode = false)
91  {
92  if (!is_object($input) && !is_array($input)) {
93  throw new \InvalidArgumentException("Input argument must be an array or object");
94  }
95  // @codingStandardsIgnoreStart
96  if ($removeItemNode && (isset($input->item) || isset($input->Map))) {
97  $node = isset($input->item) ? $input->item : $input->Map;
103  $input = is_object($node) ? [$node] : $node;
104  }
105  // @codingStandardsIgnoreEnd
106  $result = [];
107  foreach ((array)$input as $key => $value) {
108  if (is_object($value) || is_array($value)) {
109  $result[$key] = $this->convertStdObjectToArray($value, $removeItemNode);
110  } else {
111  $result[$key] = $value;
112  }
113  }
114  return $this->_unpackAssociativeArray($result);
115  }
116 
123  protected function _unpackAssociativeArray($data)
124  {
125  if (!is_array($data)) {
126  return $data;
127  } else {
128  foreach ($data as $key => $value) {
129  if (is_array($value) && count($value) == 2 && isset($value['key']) && isset($value['value'])) {
130  $data[$value['key']] = $this->_unpackAssociativeArray($value['value']);
131  unset($data[$key]);
132  } else {
133  $data[$key] = $this->_unpackAssociativeArray($value);
134  }
135  }
136  return $data;
137  }
138  }
139 
146  public static function snakeCaseToUpperCamelCase($input)
147  {
148  return str_replace(' ', '', ucwords(str_replace('_', ' ', $input)));
149  }
150 
157  public static function snakeCaseToCamelCase($input)
158  {
159  return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input))));
160  }
161 
171  public static function camelCaseToSnakeCase($name)
172  {
173  return strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
174  }
175 }
$response
Definition: 404.php:11
toFlatArray(ExtensibleDataInterface $dataObject, $dataObjectType=null)
$value
Definition: gender.phtml:16
__construct(DataObjectProcessor $dataObjectProcessor)
if(!isset($_GET['name'])) $name
Definition: log.php:14