Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataObject.php
Go to the documentation of this file.
1 <?php
13 
15 {
17  const CYCLE_DETECTED_MARK = '*** CYCLE DETECTED ***';
18 
26  public function convertDataToArray($data)
27  {
28  $result = [];
29  foreach ($data as $key => $value) {
30  if (is_object($value) || is_array($value)) {
31  $result[$key] = $this->_convertObjectToArray($value);
32  } else {
33  $result[$key] = $value;
34  }
35  }
36  return $result;
37  }
38 
46  protected function _convertObjectToArray($obj, &$objects = [])
47  {
48  $data = [];
49  if (is_object($obj)) {
50  $hash = spl_object_hash($obj);
51  if (!empty($objects[$hash])) {
53  }
54  $objects[$hash] = true;
55  if ($obj instanceof \Magento\Framework\DataObject) {
56  $data = $obj->getData();
57  } else {
58  $data = (array)$obj;
59  }
60  } elseif (is_array($obj)) {
61  $data = $obj;
62  }
63 
64  $result = [];
65  foreach ($data as $key => $value) {
66  if (is_scalar($value)) {
67  $result[$key] = $value;
68  } elseif (is_array($value)) {
69  $result[$key] = $this->_convertObjectToArray($value, $objects);
70  } elseif ($value instanceof \Magento\Framework\DataObject) {
71  $result[$key] = $this->_convertObjectToArray($value, $objects);
72  }
73  }
74  return $result;
75  }
76 
89  public function toOptionArray(array $items, $idField, $valueField)
90  {
91  $options = [];
92  foreach ($items as $item) {
93  $options[] = [
94  'value' => $this->_invokeGetter($item, $idField),
95  'label' => $this->_invokeGetter($item, $valueField),
96  ];
97  }
98  return $options;
99  }
100 
113  public function toOptionHash(array $items, $idField, $valueField)
114  {
115  $options = [];
116  foreach ($items as $item) {
117  $options[$this->_invokeGetter($item, $idField)] = $this->_invokeGetter($item, $valueField);
118  }
119  return $options;
120  }
121 
133  protected function _invokeGetter($item, $field)
134  {
135  if (is_callable($field)) {
136  // if $field is a closure, use that on the item
137  return $field($item);
138  } else {
139  // otherwise, turn it into a call to the item's getter method
140  $methodName = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
141  return $item->{$methodName}();
142  }
143  }
144 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
toOptionHash(array $items, $idField, $valueField)
Definition: DataObject.php:113
$value
Definition: gender.phtml:16
toOptionArray(array $items, $idField, $valueField)
Definition: DataObject.php:89
_convertObjectToArray($obj, &$objects=[])
Definition: DataObject.php:46
$items