-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Labels
bugSomething isn't workingSomething isn't working
Description
public function meta(string $key, $default = null)
{
$separator = '.';
$key = sprintf('%s_%s', 'woo', $key);
$data = $this->data['meta'];
// @Assert $key is a non-empty string
// @Assert $data is a loopable array
// @otherwise return $default value
if (! is_string($key) || empty($key) || ! count($data)) {
return $default;
}
// @assert $key contains a dot notated string
if (false !== strpos($key, $separator)) {
$keys = array_map(function ($key) {
if (! preg_match('/^woo_/', $key)) {
return sprintf('%s_%s', 'woo', $key);
}
return $key;
}, explode($separator, $key));
foreach ($keys as $innerKey) {
// @assert $data[$innerKey] is available to continue
// @otherwise return $default value
if (! array_key_exists($innerKey, $data)) {
return $default;
}
$data = $data[$innerKey];
}
return $data;
}
// @fallback returning value of $key in $data or $default value
$data = $data[$key] ?? $default;
// If there is a default but the types do not match, return default
if (! is_null($default) && gettype($data) !== gettype($default)) {
return $default;
}
return $data;
}
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working