b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / dev2.destoffenstraat.com / vendor / magento / framework / Convert /
Filename/home/dev2.destoffenstraat.com/vendor/magento/framework/Convert/DataObject.php
Size4.62 kb
Permissionrw-r--r--
Ownerroot : root
Create time17-Aug-2025 10:26
Last modified07-Jan-2021 21:08
Last accessed21-Aug-2025 18:30
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Default converter for \Magento\Framework\DataObjects to arrays
*
* @api
*/
namespace Magento\Framework\Convert;

class DataObject
{
/** Constant used to mark cycles in the input array/objects */
const CYCLE_DETECTED_MARK = '*** CYCLE DETECTED ***';

/**
* Convert input data into an array and return the resulting array.
* The resulting array should not contain any objects.
*
* @param array $data input data
* @return array Data converted to an array
*/
public function convertDataToArray($data)
{
$result = [];
foreach ($data as $key => $value) {
if (is_object($value) || is_array($value)) {
$result[$key] = $this->_convertObjectToArray($value);
} else {
$result[$key] = $value;
}
}
return $result;
}

/**
* Converts a \Magento\Framework\DataObject into an array, including any children objects
*
* @param mixed $obj array or object to convert
* @param array $objects array of object hashes used for cycle detection
* @return array|string Converted object or CYCLE_DETECTED_MARK
*/
protected function _convertObjectToArray($obj, &$objects = [])
{
$data = [];
if (is_object($obj)) {
$hash = spl_object_hash($obj);
if (!empty($objects[$hash])) {
return self::CYCLE_DETECTED_MARK;
}
$objects[$hash] = true;
if ($obj instanceof \Magento\Framework\DataObject) {
$data = $obj->getData();
} else {
$data = (array)$obj;
}
} elseif (is_array($obj)) {
$data = $obj;
}

$result = [];
foreach ($data as $key => $value) {
if (is_scalar($value)) {
$result[$key] = $value;
} elseif (is_array($value)) {
$result[$key] = $this->_convertObjectToArray($value, $objects);
} elseif ($value instanceof \Magento\Framework\DataObject) {
$result[$key] = $this->_convertObjectToArray($value, $objects);
}
}
return $result;
}

/**
* Converts the list of objects into an array of the form: [ [ 'label' => <id>, 'value' => <value> ], ... ].
*
*
* The <id> and <value> 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.
*
* @param array $items
* @param string|callable $idField
* @param string|callable $valueField
* @return array
*/
public function toOptionArray(array $items, $idField, $valueField)
{
$options = [];
foreach ($items as $item) {
$options[] = [
'value' => $this->_invokeGetter($item, $idField),
'label' => $this->_invokeGetter($item, $valueField),
];
}
return $options;
}

/**
* Converts the list of objects into an array of the form: [ <id> => <value>, ... ].
*
*
* The <id> and <value> 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.
*
* @param array $items
* @param string|callable $idField
* @param string|callable $valueField
* @return array
*/
public function toOptionHash(array $items, $idField, $valueField)
{
$options = [];
foreach ($items as $item) {
$options[$this->_invokeGetter($item, $idField)] = $this->_invokeGetter($item, $valueField);
}
return $options;
}

/**
* 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.
*
* @param mixed $item
* @param string|callable $field
* @return mixed
*/
protected function _invokeGetter($item, $field)
{
if (is_callable($field)) {
// if $field is a closure, use that on the item
return $field($item);
} else {
// otherwise, turn it into a call to the item's getter method
$methodName = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
return $item->{$methodName}();
}
}
}