I've stored a set of theme related configuration in a config file.
config/theme.php
// ...
$component = [
'componentA' = [
'size' => 'componentDefaultSize',
'color' => [
'default' => 'componentDefaultColor',
'success' => 'componentSuccessColor',
]
'background' => [
'light' => 'componentLightBackground',
'dark' => 'componentDarkBackground',
]
]
];
return [
// ...
'component' => $component,
];
This helper function below retrieve related values should the parameters match with keys from theme.php. The parameters are as below
string $type // Type of theme.
string $component // Name of component.
array $properties // Optional properties key name. If not defined, retrieve first array value.
function getTheme(string $type, string $component, array $properties = null)
{
$theme = null;
$styles = config("theme.$type.$component");
foreach ($styles as $style) {
if (is_array($style)) {
// If $properties exist retrieve match key value, else retrieve first value of array
if (isset($properties)) {
foreach ($style as $key => $class ) {
foreach ($properties as $property) {
if ($key == $property) {
$theme .= $class.' ';
}
}
}
} else {
$theme .= $style[0].' ';
}
} else {
$theme .= $style.' ';
}
}
return $theme;
}
I'm trying to reduce the deep nesting for helper function above but I'm not sure how to achieve that.
Example usage of the helper:
// Without properties
$theme = getTheme('component', 'componentA'); // Return all values and first value of an array
// With properties
$color = 'success';
$theme = getTheme('component', 'componentA', [$color]);
// With multiple properties
$color = 'success';
$background = 'dark';
$theme = getTheme('component', 'componentA', [$color, $background]);
Assistance is much appreciated.
Edited:
1st Attempt:
Toby's comment gave me an idea.
function getTheme(string $type, string $component, array $keys = null)
{
$theme = null;
$properties = config("theme.$type.$component");
foreach ($properties as $property) {
if (is_array($property) && isset($keys)) {
if(array_intersect(array_keys($property), $keys)){
foreach($keys as $key){
$theme .= $property[$key].' ';
}
} else {
$theme .= reset($property).' ';
}
} else if (is_array($property)) {
$theme .= reset($property).' ';
} else {
$theme .= $property.' ';
}
}
return $theme;
}
I switched $properties
to $keys
for better readability. I still don't like $theme .= reset($property).' ';
is called twice.
2nd attempt:
I've managed to refactor it to as below:
function getTheme(string $type, string $component, array $keys = null)
{
$theme = null;
$properties = config("theme.$type.$component");
foreach ($properties as $property) {
if (is_array($property) && isset($keys)) {
$theme .= implode(' ', array_values(array_intersect_key($property, array_fill_keys($keys, '')))).' ';
} else if (is_array($property)) {
$theme .= reset($property).' ';
} else {
$theme .= $property.' ';
}
}
return $theme;
}
Works fine for now but $theme .= implode(' ', array_values(array_intersect_key($property, array_fill_keys($keys, '')))).' ';
is a bit of a stretch.