Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Varien_Object::getData() and added getDataByKey() & getDataByPath() #4205

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 63 additions & 55 deletions lib/Varien/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,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 @@ -320,56 +322,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 @@ -380,7 +387,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 @@ -611,14 +618,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 @@ -635,7 +642,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, 1) . ')');
throw new Varien_Exception(
// phpcs:ignore Ecg.Security.ForbiddenFunction.Found
'Invalid method ' . get_class($this) . '::' . $method . '(' . print_r($args, 1) . ')'
);
}

/**
Expand Down Expand Up @@ -716,7 +726,6 @@ protected function _camelize($name)
*/
public function serialize($attributes = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
{
$res = '';
$data = [];
if (empty($attributes)) {
$attributes = array_keys($this->_data);
Expand All @@ -727,8 +736,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 @@ -818,7 +826,7 @@ public function offsetUnset($offset): void
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
return $this->_data[$offset] ?? null;
}

/**
Expand Down