Skip to content

Commit

Permalink
Cleanup extension class
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Oct 3, 2023
1 parent 10ee92d commit 0601a7d
Showing 1 changed file with 18 additions and 41 deletions.
59 changes: 18 additions & 41 deletions src/DependencyInjection/NeustaConverterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,54 +78,31 @@ private function registerConverterConfiguration(string $id, array $config, Conta
* @param array<string, mixed> $config
*/
private function registerPopulatorConfiguration(string $id, array $config, ContainerBuilder $container): void
{
$container->register($id, $config['populator'])
->setPublic(true)
->setArguments(match ($config['populator']) {
ConvertingPopulator::class => $this->buildArgumentsForConvertingPopulator($config),
ArrayConvertingPopulator::class => $this->buildArgumentsForArrayConvertingPopulator($config),
default => [],
});
}

/**
* @param array<string, mixed> $config
*
* @return array<string, string>
*/
private function buildArgumentsForConvertingPopulator(array $config): array
{
$targetProperty = array_key_first($config['property']);
$sourceProperty = $config['property'][$targetProperty] ?? $targetProperty;

return [
'$converter' => new TypedReference($config['converter'], Converter::class),
'$sourcePropertyName' => $sourceProperty,
'$targetPropertyName' => $targetProperty,
'$accessor' => new Reference('property_accessor'),
];
}

/**
* @param array<string, mixed> $config
*
* @return array<string, string>
*/
private function buildArgumentsForArrayConvertingPopulator(array $config): array
{
$itemProperty = $config['property']['itemProperty'] ?? null;
unset($config['property']['itemProperty']);

$targetProperty = array_key_first($config['property']);
$sourceProperty = $config['property'][$targetProperty] ?? $targetProperty;

return [
'$converter' => new TypedReference($config['converter'], Converter::class),
'$sourceArrayPropertyName' => $sourceProperty,
'$targetPropertyName' => $targetProperty,
'$sourceArrayItemPropertyName' => $itemProperty,
'$accessor' => new Reference('property_accessor'),
];
$container->register($id, $config['populator'])
->setPublic(true)
->setArguments(match ($config['populator']) {
ConvertingPopulator::class => [
'$converter' => new TypedReference($config['converter'], Converter::class),
'$sourcePropertyName' => $sourceProperty,
'$targetPropertyName' => $targetProperty,
'$accessor' => new Reference('property_accessor'),
],
ArrayConvertingPopulator::class => [
'$converter' => new TypedReference($config['converter'], Converter::class),
'$sourceArrayPropertyName' => $sourceProperty,
'$targetPropertyName' => $targetProperty,
'$sourceArrayItemPropertyName' => $itemProperty,
'$accessor' => new Reference('property_accessor'),
],
default => [],
});
}

private function appendSuffix(string $value, string $suffix): string
Expand Down

0 comments on commit 0601a7d

Please sign in to comment.