private function applyLegend(array &$config, array $action)
{
// Legend already exists, do nothing
if (array_key_exists($action['name'], $config)) {
return;
}
$template = [$action['name'] => ['fields' => [], 'hide' => $action['hide']]];
if (self::POSITION_PREPEND === $action['position']) {
$config = $template + $config;
return;
}
if (self::POSITION_APPEND === $action['position']) {
$config += $template;
return;
}
foreach ($action['parents'] as $parent) {
if (array_key_exists($parent, $config)) {
$offset = array_search($parent, array_keys($config), true);
$offset += (int) (self::POSITION_AFTER === $action['position']);
// Necessary because array_splice() would remove the keys from the replacement array
$before = array_splice($config, 0, $offset);
$config = $before + $template + $config;
return;
}
}
// If everything fails, append the new legend at the end
$config += $template;
}