Skip to content

Commit

Permalink
Fixup #53 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen authored Nov 12, 2023
1 parent 8fdac78 commit 0c597ab
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 93 deletions.
25 changes: 0 additions & 25 deletions config/packages/neusta_converter.yaml

This file was deleted.

13 changes: 6 additions & 7 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Neusta\ConverterBundle\DependencyInjection;

use Neusta\ConverterBundle\Converter\GenericConverter;
use Neusta\ConverterBundle\Populator;
use Neusta\ConverterBundle\Populator\ConvertingPopulator;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -92,15 +92,15 @@ private function addPopulatorSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->arrayNode('populators')
->arrayNode('populator')
->info('Populator configuration')
->normalizeKeys(false)
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->scalarNode('class')
->scalarNode('populator')
->info('class of the "Populator" implementation')
->defaultValue(Populator\ConvertingPopulator::class)
->defaultValue(ConvertingPopulator::class)
->end()
->scalarNode('converter')
->info('Service id of the internal "Converter"')
Expand All @@ -112,13 +112,12 @@ private function addPopulatorSection(ArrayNodeDefinition $rootNode): void
->normalizeKeys(false)
->useAttributeAsKey('target')
->prototype('scalar')
->isRequired()
->end()
->end()
->end()
->validate()
->ifTrue(fn (array $c) => empty($c['converter']) && empty($c['property']))
->thenInvalid('A "converter" and "property" must be defined.')
->ifTrue(fn (array $c) => empty($c['property']))
->thenInvalid('At least one "property" must be defined.')
->end()
->end()
->end()
Expand Down
70 changes: 22 additions & 48 deletions src/DependencyInjection/NeustaConverterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function loadInternal(array $mergedConfig, ContainerBuilder $container):
$this->registerConverterConfiguration($converterId, $converter, $container);
}

foreach ($mergedConfig['populators'] as $populatorId => $populator) {
foreach ($mergedConfig['populator'] as $populatorId => $populator) {
$this->registerPopulatorConfiguration($populatorId, $populator, $container);
}
}
Expand Down Expand Up @@ -80,56 +80,30 @@ private function registerConverterConfiguration(string $id, array $config, Conta
*/
private function registerPopulatorConfiguration(string $id, array $config, ContainerBuilder $container): void
{
$arguments = [];
if (empty($config['class']) || ConvertingPopulator::class === $config['class']) {
$arguments = $this->buildArgumentsForConvertingPopulator($config);
} elseif (ArrayConvertingPopulator::class === $config['class']) {
$arguments = $this->buildArgumentsForArrayConvertingPopulator($config);
}

$container->register($id, $config['class'])
->setPublic(true)
->setArguments($arguments);
}

/**
* @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];
$sourceProperty = $sourceProperty ?? $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
{
$innerPropertyArgument = [];
$innerProperty = $config['property']['itemProperty'];
$innerPropertyArgument['$sourceArrayItemPropertyName'] = $innerProperty;
$itemProperty = $config['property']['itemProperty'] ?? null;
unset($config['property']['itemProperty']);

$arguments = array_merge(
$innerPropertyArgument,
$this->buildArgumentsForConvertingPopulator($config),
);
$targetProperty = array_key_first($config['property']);
$sourceProperty = $config['property'][$targetProperty] ?? $targetProperty;

return $arguments;
$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
27 changes: 15 additions & 12 deletions tests/DependencyInjection/NeustaConverterExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function test_with_mapped_context(): void
public function test_with_converting_populator(): void
{
$container = $this->buildContainer([
'populators' => [
'populator' => [
'foobar' => [
'converter' => GenericConverter::class,
'property' => [
Expand All @@ -155,15 +155,17 @@ public function test_with_converting_populator(): void
self::assertSame(ConvertingPopulator::class, $populator->getClass());
self::assertTrue($populator->isPublic());
self::assertInstanceOf(TypedReference::class, $populator->getArgument('$converter'));
self::assertSame(GenericConverter::class, (string) $populator->getArgument('$converter'));
self::assertSame('targetTest', $populator->getArgument('$targetPropertyName'));
self::assertSame('sourceTest', $populator->getArgument('$sourcePropertyName'));
}

public function test_with_array_converting_populator(): void
{
$container = $this->buildContainer([
'populators' => [
'populator' => [
'foobar' => [
'populator' => ArrayConvertingPopulator::class,
'converter' => GenericConverter::class,
'property' => [
'targetTest' => 'sourceTest',
Expand All @@ -175,19 +177,20 @@ public function test_with_array_converting_populator(): void
// converter
$populator = $container->getDefinition('foobar');

self::assertSame(ConvertingPopulator::class, $populator->getClass());
self::assertSame(ArrayConvertingPopulator::class, $populator->getClass());
self::assertTrue($populator->isPublic());
self::assertInstanceOf(TypedReference::class, $populator->getArgument('$converter'));
self::assertSame(GenericConverter::class, (string) $populator->getArgument('$converter'));
self::assertSame('targetTest', $populator->getArgument('$targetPropertyName'));
self::assertSame('sourceTest', $populator->getArgument('$sourcePropertyName'));
self::assertSame('sourceTest', $populator->getArgument('$sourceArrayPropertyName'));
}

public function test_with_array_converting_populator_with_inner_property(): void
{
$container = $this->buildContainer([
'populators' => [
'populator' => [
'foobar' => [
'class' => ArrayConvertingPopulator::class,
'populator' => ArrayConvertingPopulator::class,
'converter' => GenericConverter::class,
'property' => [
'targetTest' => 'sourceTest',
Expand All @@ -207,9 +210,9 @@ public function test_with_array_converting_populator_with_inner_property(): void
public function test_with_array_converting_populator_with_inner_property_same_name(): void
{
$container = $this->buildContainer([
'populators' => [
'populator' => [
'foobar' => [
'class' => ArrayConvertingPopulator::class,
'populator' => ArrayConvertingPopulator::class,
'converter' => GenericConverter::class,
'property' => [
'test' => null, // in yaml one will write ~
Expand All @@ -224,16 +227,16 @@ public function test_with_array_converting_populator_with_inner_property_same_na

self::assertSame(ArrayConvertingPopulator::class, $populator->getClass());
self::assertSame('test', $populator->getArgument('$targetPropertyName'));
self::assertSame('test', $populator->getArgument('$sourcePropertyName'));
self::assertSame('test', $populator->getArgument('$sourceArrayPropertyName'));
self::assertSame('value', $populator->getArgument('$sourceArrayItemPropertyName'));
}

public function test_with_array_converting_populator_with_inner_property_first(): void
{
$container = $this->buildContainer([
'populators' => [
'populator' => [
'foobar' => [
'class' => ArrayConvertingPopulator::class,
'populator' => ArrayConvertingPopulator::class,
'converter' => GenericConverter::class,
'property' => [
'itemProperty' => 'value',
Expand All @@ -248,7 +251,7 @@ public function test_with_array_converting_populator_with_inner_property_first()

self::assertSame(ArrayConvertingPopulator::class, $populator->getClass());
self::assertSame('targetTest', $populator->getArgument('$targetPropertyName'));
self::assertSame('sourceTest', $populator->getArgument('$sourcePropertyName'));
self::assertSame('sourceTest', $populator->getArgument('$sourceArrayPropertyName'));
self::assertSame('value', $populator->getArgument('$sourceArrayItemPropertyName'));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/app/config/packages/neusta_converter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ neusta_converter:
properties:
phoneNumber: number

populators:
populator:
test.person.address.populator:
converter: test.address.converter
property:
Expand Down

0 comments on commit 0c597ab

Please sign in to comment.