I have some default configurations, and some specific configurations which would be configurable. I need to merge the specific configurations into the default configurations.
- In the case that the specific config option does not exist, the default option will be used.
- In the case that the
value
is a scalar, the specific configuration should be applied - In the case that the
value
is a scalar array, the arrays should be merged and array_unique applied. - In the case that the
value
is an associative array, We need to apply the abovescalar
andscalar_array
rules.
Example:
$defaultConfigs = [
'scalar1' => 1,
'scalar2' => "Apple",
'array_scalar' => [3,4,5],
'array_associative' => [
'scalar' => 1,
'array_scalar' => [1,2,3],
'array_associative' => [
...
]
],
];
$specificConfigs = [
'scalar1' => "A",
'array_scalar' => [3,4,5],
'array_associative' => [
'scalar' => 1,
'array_scalar' => [1,2,3],
'array_associative' => [
...
]
],
];
Expected Output:
$expectedConfigs = [
'scalar1' => "A", // Overridden
'scalar2' => "Apple", // Default used
'array_scalar' => [1,2,3,4,5], // Scalar merged and array_unique
'array_associative' => [
'scalar' => "B", // Overridden
'array_scalar' => [1,2,3,4,5], // Scalar merged and array_unique
'array_associative' => [
...
]
],
];
Is there a nice clean way of achieving this?