Skip to content

Commit

Permalink
OM PR 4205
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Sep 18, 2024
1 parent 0120f15 commit e56e1ae
Showing 1 changed file with 63 additions and 55 deletions.
118 changes: 63 additions & 55 deletions lib/Varien/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,15 @@ public function unsetOldData($key = null)
}

/**
* Retrieves data from the object
* Object data getter
*
* If $key is empty will return all the data as an array
* Otherwise it will return value of the attribute specified by $key
* If $key is not defined will return all the data as an array.
* Otherwise, it will return value of the element specified by $key.
* It is possible to use keys like a/b/c for access nested array data
*
* If $index is specified it will assume that attribute data is an array
* and retrieve corresponding member.
* and retrieve corresponding member. If data is the string - it will be explode
* by new line character and converted to array.
*
* @param string $key
* @param string|int $index
Expand All @@ -316,56 +318,61 @@ public function getData($key = '', $index = null)
return $this->_data;
}

$default = null;

// accept a/b/c as ['a']['b']['c']
if (strpos($key, '/')) {
$keyArr = explode('/', $key);
$data = $this->_data;
foreach ($keyArr as $i => $k) {
if ($k === '') {
return $default;
}
if (is_array($data)) {
if (!isset($data[$k])) {
return $default;
}
$data = $data[$k];
} elseif ($data instanceof Varien_Object) {
$data = $data->getData($k);
} else {
return $default;
}
}
return $data;
$data = $this->_data[$key] ?? null;
if ($data === null && $key !== null && strpos($key, '/') !== false) {
/* process a/b/c key as ['a']['b']['c'] */
$data = $this->getDataByPath($key);
}

// legacy functionality for $index
if (isset($this->_data[$key])) {
if (is_null($index)) {
return $this->_data[$key];
if ($index !== null) {
if ($data === (array)$data) {
$data = $data[$index] ?? null;
} elseif (is_string($data)) {
$data = explode(PHP_EOL, $data);
$data = $data[$index] ?? null;
} elseif ($data instanceof Varien_Object) {
$data = $data->getData($index);
} else {
$data = null;
}
}
return $data;
}

/**
* Get object data by path
*
* Method consider the path as chain of keys: a/b/c => ['a']['b']['c']
*
* @param string $path
* @return mixed
*/
public function getDataByPath($path)
{
$keys = explode('/', (string)$path);

$value = $this->_data[$key];
if (is_array($value)) {
//if (isset($value[$index]) && (!empty($value[$index]) || strlen($value[$index]) > 0)) {
/**
* If we have any data, even if it empty - we should use it, anyway
*/
if (isset($value[$index])) {
return $value[$index];
}
$data = $this->_data;
foreach ($keys as $key) {
if ((array)$data === $data && isset($data[$key])) {
$data = $data[$key];
} elseif ($data instanceof Varien_Object) {
$data = $data->getDataByKey($key);
} else {
return null;
} elseif (is_string($value)) {
$arr = explode("\n", $value);
return (isset($arr[$index]) && (!empty($arr[$index]) || strlen($arr[$index]) > 0))
? $arr[$index] : null;
} elseif ($value instanceof Varien_Object) {
return $value->getData($index);
}
return $default;
}
return $default;
return $data;
}

/**
* Get object data by particular key
*
* @param string $key
* @return mixed
*/
public function getDataByKey($key)
{
return $this->_getData($key);
}

/**
Expand All @@ -376,7 +383,7 @@ public function getData($key = '', $index = null)
*/
protected function _getData($key)
{
return isset($this->_data[$key]) ? $this->_data[$key] : null;
return $this->_data[$key] ?? null;
}

/**
Expand Down Expand Up @@ -603,14 +610,14 @@ public function __call($method, $args)
case 'get':
//Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method, 3));
$data = $this->getData($key, isset($args[0]) ? $args[0] : null);
$data = $this->getData($key, $args[0] ?? null);
//Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
return $data;

case 'set':
//Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method, 3));
$result = $this->setData($key, isset($args[0]) ? $args[0] : null);
$result = $this->setData($key, $args[0] ?? null);
//Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
return $result;

Expand All @@ -627,7 +634,10 @@ public function __call($method, $args)
//Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
return isset($this->_data[$key]);
}
throw new Varien_Exception('Invalid method ' . get_class($this) . '::' . $method . '(' . print_r($args, true) . ')');
throw new Varien_Exception(
// phpcs:ignore Ecg.Security.ForbiddenFunction.Found
'Invalid method ' . get_class($this) . '::' . $method . '(' . print_r($args, 1) . ')'
);
}

/**
Expand Down Expand Up @@ -708,7 +718,6 @@ protected function _camelize($name)
*/
public function serialize($attributes = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
{
$res = '';
$data = [];
if (empty($attributes)) {
$attributes = array_keys($this->_data);
Expand All @@ -719,8 +728,7 @@ public function serialize($attributes = [], $valueSeparator = '=', $fieldSeparat
$data[] = $key . $valueSeparator . $quote . $value . $quote;
}
}
$res = implode($fieldSeparator, $data);
return $res;
return implode($fieldSeparator, $data);
}

/**
Expand Down Expand Up @@ -813,7 +821,7 @@ public function offsetUnset($offset): void
#[\Override]
public function offsetGet($offset)
{
return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
return $this->_data[$offset] ?? null;
}

/**
Expand Down

0 comments on commit e56e1ae

Please sign in to comment.