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

Public Member Functions

 convertDataToArray ($data)
 
 toOptionArray (array $items, $idField, $valueField)
 
 toOptionHash (array $items, $idField, $valueField)
 

Data Fields

const CYCLE_DETECTED_MARK = '*** CYCLE DETECTED ***'
 

Protected Member Functions

 _convertObjectToArray ($obj, &$objects=[])
 
 _invokeGetter ($item, $field)
 

Detailed Description

Definition at line 14 of file DataObject.php.

Member Function Documentation

◆ _convertObjectToArray()

_convertObjectToArray (   $obj,
$objects = [] 
)
protected

Converts a \Magento\Framework\DataObject into an array, including any children objects

Parameters
mixed$objarray or object to convert
array$objectsarray of object hashes used for cycle detection
Returns
array|string Converted object or CYCLE_DETECTED_MARK

Definition at line 46 of file DataObject.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
_convertObjectToArray($obj, &$objects=[])
Definition: DataObject.php:46

◆ _invokeGetter()

_invokeGetter (   $item,
  $field 
)
protected

Returns the value of the property represented by $field on the $item object.

When $field is a closure, the $item parameter is passed to the $field method, otherwise the $field is assumed to be a property name, and the associated get method is invoked on the $item instead.

Parameters
mixed$item
string | callable$field
Returns
mixed

Definition at line 133 of file DataObject.php.

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  }

◆ convertDataToArray()

convertDataToArray (   $data)

Convert input data into an array and return the resulting array. The resulting array should not contain any objects.

Parameters
array$datainput data
Returns
array Data converted to an array

Definition at line 26 of file DataObject.php.

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  }
$value
Definition: gender.phtml:16
_convertObjectToArray($obj, &$objects=[])
Definition: DataObject.php:46

◆ toOptionArray()

toOptionArray ( array  $items,
  $idField,
  $valueField 
)

Converts the list of objects into an array of the form: [ [ 'label' => <id>, 'value' =>

], ... ].

The <id> and

values are taken from the objects in the list using the $idField and $valueField parameters, which can be either the name of the field to use, or a closure.

Parameters
array$items
string | callable$idField
string | callable$valueField
Returns
array

Definition at line 89 of file DataObject.php.

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  }
$items

◆ toOptionHash()

toOptionHash ( array  $items,
  $idField,
  $valueField 
)

Converts the list of objects into an array of the form: [ <id> =>

, ... ].

The <id> and

values are taken from the objects in the list using the $idField and $valueField parameters, which can be either the name of the field to use, or a closure.

Parameters
array$items
string | callable$idField
string | callable$valueField
Returns
array

Definition at line 113 of file DataObject.php.

114  {
115  $options = [];
116  foreach ($items as $item) {
117  $options[$this->_invokeGetter($item, $idField)] = $this->_invokeGetter($item, $valueField);
118  }
119  return $options;
120  }
$items

Field Documentation

◆ CYCLE_DETECTED_MARK

const CYCLE_DETECTED_MARK = '*** CYCLE DETECTED ***'

Constant used to mark cycles in the input array/objects

Definition at line 17 of file DataObject.php.


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