diff --git a/README.md b/README.md index 33b8e3c..3846e8d 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ This bundle comes with several attributes that you can use to add mapping to you - Use it as a Property attribute if you have some use for it ([see example](tests/Examples/Valid/Complex/CustomerDTO.php)). - Specify the mapped property name directly on the attribute ([see example](tests/Examples/Valid/Complex/InvoiceDTO.php)). This is mandatory when used as a Class attribute. - Specify the mapped property name separately with the `InboundProperty` attribute, Doctrine-style ([see example](tests/Examples/Valid/ReferencesArray/RootDTO.php)). -- `#[InboundProperty("mapped_property_name")]`: The name of the key on the associative arrays contained by your result set. This is optional if your DTO's property names are already matching the result set. +- `#[InboundProperty("mapped_property_name")]`: The name of the key on the associative arrays contained by your result set. This is optional if your DTO's property names are already matching the result set ([see example](tests/Examples/Valid/WithoutAttributeDTO.php)). - `#[ReferencesArray(NestedDTO::class)]`: An array of `NestedDTO` will be created using the mapping information contained in `NestedDTO`. - `#[ColumnArray("mapped_property_name")]` the column `mapped_property_name` of your result set will be mapped as an array of scalar properties (such as IDs). diff --git a/tests/Examples/Valid/WithoutAttributeDTO.php b/tests/Examples/Valid/WithoutAttributeDTO.php new file mode 100644 index 0000000..e7bf51c --- /dev/null +++ b/tests/Examples/Valid/WithoutAttributeDTO.php @@ -0,0 +1,16 @@ +assertEquals(138619, $customerDto->getTotalPurchases()); } + public function testMapWithoutAttributeDTO(): void + { + $results = [ + ['id' => 1, 'foo' => 'Foo 1', 'bar' => 1], + ['id' => 2, 'foo' => 'Foo 2', 'bar' => 2], + ]; + + $flatMapperResults = ((new FlatMapper())->map(WithoutAttributeDTO::class, $results)); + + $rootDto1 = new WithoutAttributeDTO(1, "Foo 1", 1); + $rootDto2 = new WithoutAttributeDTO(2, "Foo 2", 2); + $handmadeResult = [1 => $rootDto1, 2 => $rootDto2]; + + $this->assertSame( + var_export($flatMapperResults, true), + var_export($handmadeResult, true) + ); + } + public function testMapEmptyData(): void { $flatMapperResults = ((new FlatMapper())->map(ColumnArrayDTO::class, []));