Skip to content

Commit

Permalink
#68 rename property to skip_null
Browse files Browse the repository at this point in the history
add test for Extension class
  • Loading branch information
mike4git committed Apr 2, 2024
1 parent 2c7e776 commit b6b73af
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ private function addConverterSection(ArrayNodeDefinition $rootNode): void
->arrayPrototype()
->beforeNormalization()
->ifNull()
->then(fn () => ['source' => null, 'default' => null, 'nullsafe' => false])
->then(fn () => ['source' => null, 'default' => null, 'skip_null' => false])
->end()
->beforeNormalization()
->ifString()
->then(fn (string $v) => ['source' => $v, 'default' => null, 'nullsafe' => false])
->then(fn (string $v) => ['source' => $v, 'default' => null, 'skip_null' => false])
->end()
->children()
->scalarNode('source')
Expand All @@ -69,7 +69,7 @@ private function addConverterSection(ArrayNodeDefinition $rootNode): void
->scalarNode('default')
->defaultValue(null)
->end()
->booleanNode('nullsafe')
->booleanNode('skip_null')
->defaultFalse()
->end()
->end()
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/NeustaConverterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function loadInternal(array $mergedConfig, ContainerBuilder $container):
private function registerConverterConfiguration(string $id, array $config, ContainerBuilder $container): void
{
foreach ($config['properties'] ?? [] as $targetProperty => $sourceConfig) {
$nullSafety = false;
$skip_null = false;
if (str_ends_with($targetProperty, '?')) {
$nullSafety = true;
$skip_null = true;
$targetProperty = substr($targetProperty, 0, -1);
}
$config['populators'][] = $propertyPopulatorId = "{$id}.populator.{$targetProperty}";
Expand All @@ -55,7 +55,7 @@ private function registerConverterConfiguration(string $id, array $config, Conta
'$defaultValue' => $sourceConfig['default'] ?? null,
'$mapper' => null,
'$accessor' => new Reference('property_accessor'),
'$nullSafety' => $sourceConfig['nullsafe'] ?? $nullSafety,
'$skip_null' => $sourceConfig['skip_null'] || $skip_null,
]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Populator/PropertyMappingPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
private mixed $defaultValue = null,
?\Closure $mapper = null,
?PropertyAccessorInterface $accessor = null,
private bool $nullSafety = false,
private bool $skip_null = false,
) {
$this->mapper = $mapper ?? static fn ($v) => $v;
$this->accessor = $accessor ?? PropertyAccess::createPropertyAccessor();
Expand All @@ -45,7 +45,7 @@ public function populate(object $target, object $source, ?object $ctx = null): v
try {
$value = $this->accessor->getValue($source, $this->sourceProperty) ?? $this->defaultValue;

if (!$this->nullSafety || (null !== $value)) {
if (!$this->skip_null || (null !== $value)) {
$this->accessor->setValue($target, $this->targetProperty, ($this->mapper)($value, $ctx));
}
} catch (\Throwable $exception) {
Expand Down
18 changes: 18 additions & 0 deletions tests/DependencyInjection/NeustaConverterExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function test_with_mapped_properties(): void
'email' => [
'source' => 'mail',
],
'fullName?' => null,
],
],
],
Expand All @@ -71,13 +72,30 @@ public function test_with_mapped_properties(): void
new Reference('foobar.populator.ageInYears'),
new Reference('foobar.populator.email'),
]);
$converter = $container->getDefinition('foobar');
self::assertSame(GenericConverter::class, $converter->getClass());
self::assertTrue($converter->isPublic());
self::assertTrue($container->hasAlias(Converter::class . ' $foobarConverter'));
self::assertIsReference(PersonFactory::class, $converter->getArgument('$factory'));
self::assertIsArray($converter->getArgument('$populators'));
self::assertCount(4, $converter->getArgument('$populators'));
self::assertIsReference('foobar.populator.name', $converter->getArgument('$populators')[0]);
self::assertIsReference('foobar.populator.ageInYears', $converter->getArgument('$populators')[1]);

// name property populator
$this->assertContainerBuilderHasService('foobar.populator.name', PropertyMappingPopulator::class);
$this->assertContainerBuilderHasServiceDefinitionWithArgument('foobar.populator.name', '$accessor', new Reference('property_accessor'));
$this->assertContainerBuilderHasServiceDefinitionWithArgument('foobar.populator.name', '$targetProperty', 'name');
$this->assertContainerBuilderHasServiceDefinitionWithArgument('foobar.populator.name', '$sourceProperty', 'name');

// name property populator
$fullNamePopulator = $container->getDefinition('foobar.populator.fullName');
self::assertSame(PropertyMappingPopulator::class, $fullNamePopulator->getClass());
self::assertIsReference('property_accessor', $fullNamePopulator->getArgument('$accessor'));
self::assertSame('fullName', $fullNamePopulator->getArgument('$targetProperty'));
self::assertSame('fullName', $fullNamePopulator->getArgument('$sourceProperty'));
self::assertTrue($fullNamePopulator->getArgument('$skip_null'));

// ageInYears property populator
$this->assertContainerBuilderHasService('foobar.populator.ageInYears', PropertyMappingPopulator::class);
$this->assertContainerBuilderHasServiceDefinitionWithArgument('foobar.populator.ageInYears', '$accessor', new Reference('property_accessor'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Config/person.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ neusta_converter:
fullName:
source: fullName
default: 'Hans Herrmann'
nullsafe: true
skip_null: true
age?: ageInYears

test.contactnumber.converter:
Expand Down

0 comments on commit b6b73af

Please sign in to comment.