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() #3245

Closed
wants to merge 4 commits into from
Closed
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
109 changes: 58 additions & 51 deletions lib/Varien/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data is a string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh, im no native speaker, but "the" seem correct here.

Hhowever ... can you please use github suggestion function? (ctrl-g)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording in that context is not exactly appropriate. It is not an error to turn this PR upside down, it can reformulated. As I can live with some of the OM translations, I can live with this wording.

* by new line character and converted to array.
*
* @param string $key
* @param string|int $index
Expand All @@ -318,56 +320,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 @@ -378,7 +385,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 @@ -609,14 +616,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 Down Expand Up @@ -816,7 +823,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