Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add a better example #3

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ This bundle comes with several attributes that you can use to add mapping to you
- Use it as a Class attribute if you don't intend to use the property yourself ([see example](tests/Examples/Valid/Complex/ProductDTO.php)). It will then only be used internally and not be mapped to your DTO.
- 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)).
- Specify the mapped property name separately with the `InboundProperty` attribute, Doctrine-style ([see example](tests/Examples/Valid/ReferencesArray/AuthorDTO.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 ([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).
Expand All @@ -111,18 +111,18 @@ This bundle comes with several attributes that you can use to add mapping to you

Given:

- [RootDTO](tests/Examples/Valid/ReferencesArray/RootDTO.php)
- [LeafDTO](tests/Examples/Valid/ReferencesArray/LeafDTO.php)
- [AuthorDTO](tests/Examples/Valid/ReferencesArray/AuthorDTO.php)
- [BookDTO](tests/Examples/Valid/ReferencesArray/BookDTO.php)

Calling FlatMapper with the following result set:

```php
$results = [
['object1_id' => 1, 'object1_name' => 'Root 1', 'object2_id' => 1, 'object2_name' => 'Leaf 1', 'object2_value' => 'Value 1'],
['object1_id' => 1, 'object1_name' => 'Root 1', 'object2_id' => 2, 'object2_name' => 'Leaf 2', 'object2_value' => 'Value 2'],
['object1_id' => 1, 'object1_name' => 'Root 1', 'object2_id' => 3, 'object2_name' => 'Leaf 3', 'object2_value' => 'Value 3'],
['object1_id' => 2, 'object1_name' => 'Root 2', 'object2_id' => 1, 'object2_name' => 'Leaf 1', 'object2_value' => 'Value 1'],
['object1_id' => 2, 'object1_name' => 'Root 2', 'object2_id' => 4, 'object2_name' => 'Leaf 4', 'object2_value' => 'Value 4'],
['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 1, 'book_name' => 'Travelling as a group', 'book_publisher_name' => 'TravelBooks'],
['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 2, 'book_name' => 'My journeys', 'book_publisher_name' => 'Lorem Press'],
['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 3, 'book_name' => 'Coding on the road', 'book_publisher_name' => 'Ipsum Books'],
['author_id' => 2, 'author_name' => 'Bob Schmo', 'book_id' => 1, 'book_name' => 'Travelling as a group', 'book_publisher_name' => 'TravelBooks'],
['author_id' => 2, 'author_name' => 'Bob Schmo', 'book_id' => 4, 'book_name' => 'My best recipes', 'book_publisher_name' => 'Cooking and Stuff'],
];

$flatMapper->map(RootDTO::class, $results);
Expand All @@ -133,51 +133,49 @@ Will output:
```php
Array
(
[1] => RootDTO Object
[1] => AuthorDTO Object
(
[id] => 1
[name] => Root 1
[name] => Alice Brian
[leafs] => Array
(
[1] => LeafDTO Object
[1] => BookDTO Object
(
[id] => 1
[name] => Leaf 1
[value] => Value 1
[name] => "Travelling as a group"
[publisherName] => "TravelBooks"
)

[2] => LeafDTO Object
[2] => BookDTO Object
(
[id] => 2
[name] => Leaf 2
[value] => Value 2
[name] => "My journeys"
[publisherName] => "Lorem Press"
)

[3] => LeafDTO Object
[3] => BookDTO Object
(
[id] => 3
[name] => Leaf 3
[value] => Value 3
[name] => "Coding on the road"
[publisherName] => "Ipsum Books"
)
)
)
[2] => RootDTO Object
[2] => AuthorDTO Object
(
[id] => 2
[name] => Root 2
[name] => Bob Schmo
[leafs] => Array
(
[1] => LeafDTO Object
[1] => BookDTO Object
(
[id] => 1
[name] => Leaf 1
[value] => Value 1
[name] => "Travelling as a group"
[publisherName] => "TravelBooks"
)
[4] => LeafDTO Object
[4] => BookDTO Object
(
[id] => 4
[name] => Leaf 4
[value] => Value 4
[name] => "My best recipes"
[publisherName] => "Cooking and Stuff"
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
use Pixelshaped\FlatMapperBundle\Attributes\InboundPropertyName;
use Pixelshaped\FlatMapperBundle\Attributes\ReferencesArray;

class RootDTO
class AuthorDTO
{
/**
* @param array<LeafDTO> $leafs
* @param array<BookDTO> $leafs
*/
public function __construct(
#[Identifier]
#[InboundPropertyName('object1_id')]
#[InboundPropertyName('author_id')]
public int $id,
#[InboundPropertyName('object1_name')]
#[InboundPropertyName('author_name')]
public string $name,
#[ReferencesArray(LeafDTO::class)]
#[ReferencesArray(BookDTO::class)]
public array $leafs,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
use Pixelshaped\FlatMapperBundle\Attributes\Identifier;
use Pixelshaped\FlatMapperBundle\Attributes\InboundPropertyName;

class LeafDTO
class BookDTO
{
public function __construct(
#[Identifier]
#[InboundPropertyName('object2_id')]
#[InboundPropertyName('book_id')]
public int $id,
#[InboundPropertyName('object2_name')]
#[InboundPropertyName('book_name')]
public string $name,
#[InboundPropertyName('object2_value')]
public string $value,
#[InboundPropertyName('book_publisher_name')]
public string $publisherName,
) {}
}
40 changes: 20 additions & 20 deletions tests/FlatMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\Complex\CustomerDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\Complex\InvoiceDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\Complex\ProductDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ReferencesArray\LeafDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ReferencesArray\RootDTO as ValidRootDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ReferencesArray\AuthorDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ReferencesArray\BookDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\WithoutAttributeDTO;
use RuntimeException;

Expand All @@ -29,7 +29,7 @@ public function testCreateMappingWithValidDTOsDoesNotAssert(): void
$this->expectNotToPerformAssertions();
$mapper = new FlatMapper();
$mapper->createMapping(ColumnArrayDTO::class);
$mapper->createMapping(ValidRootDTO::class);
$mapper->createMapping(AuthorDTO::class);
}

public function testCreateMappingWithSeveralIdenticalIdentifiersAsserts(): void
Expand Down Expand Up @@ -75,29 +75,29 @@ public function testCreateMappingWithEmptyClassIdentifierAsserts(): void
public function testMapValidNestedDTOs(): void
{
$results = [
['object1_id' => 1, 'object1_name' => 'Root 1', 'object2_id' => 1, 'object2_name' => 'Leaf 1', 'object2_value' => 'Value 1'],
['object1_id' => 1, 'object1_name' => 'Root 1', 'object2_id' => 2, 'object2_name' => 'Leaf 2', 'object2_value' => 'Value 2'],
['object1_id' => 1, 'object1_name' => 'Root 1', 'object2_id' => 3, 'object2_name' => 'Leaf 3', 'object2_value' => 'Value 3'],
['object1_id' => 2, 'object1_name' => 'Root 2', 'object2_id' => 1, 'object2_name' => 'Leaf 1', 'object2_value' => 'Value 1'],
['object1_id' => 2, 'object1_name' => 'Root 2', 'object2_id' => 4, 'object2_name' => 'Leaf 4', 'object2_value' => 'Value 4'],
['object1_id' => 5, 'object1_name' => 'Root 5', 'object2_id' => null, 'object2_name' => null, 'object2_value' => null],
['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 1, 'book_name' => 'Travelling as a group', 'book_publisher_name' => 'TravelBooks'],
['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 2, 'book_name' => 'My journeys', 'book_publisher_name' => 'Lorem Press'],
['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 3, 'book_name' => 'Coding on the road', 'book_publisher_name' => 'Ipsum Books'],
['author_id' => 2, 'author_name' => 'Bob Schmo', 'book_id' => 1, 'book_name' => 'Travelling as a group', 'book_publisher_name' => 'TravelBooks'],
['author_id' => 2, 'author_name' => 'Bob Schmo', 'book_id' => 4, 'book_name' => 'My best recipes', 'book_publisher_name' => 'Cooking and Stuff'],
['author_id' => 5, 'author_name' => 'Charlie Doe', 'book_id' => null, 'book_name' => null, 'book_publisher_name' => null],
];

$flatMapperResults = ((new FlatMapper())->map(ValidRootDTO::class, $results));
$flatMapperResults = ((new FlatMapper())->map(AuthorDTO::class, $results));

$leafDto1 = new LeafDTO(1, "Leaf 1", "Value 1");
$leafDto2 = new LeafDTO(2, "Leaf 2", "Value 2");
$leafDto3 = new LeafDTO(3, "Leaf 3", "Value 3");
$leafDto4 = new LeafDTO(4, "Leaf 4", "Value 4");
$bookDto1 = new BookDTO(1, "Travelling as a group", "TravelBooks");
$bookDto2 = new BookDTO(2, "My journeys", "Lorem Press");
$bookDto3 = new BookDTO(3, "Coding on the road", "Ipsum Books");
$bookDto4 = new BookDTO(4, "My best recipes", "Cooking and Stuff");

$rootDto1 = new ValidRootDTO(1, "Root 1", [
1 => $leafDto1, 2 => $leafDto2, 3 => $leafDto3
$authorDto1 = new AuthorDTO(1, "Alice Brian", [
1 => $bookDto1, 2 => $bookDto2, 3 => $bookDto3
]);
$rootDto2 = new ValidRootDTO(2, "Root 2", [
1 => $leafDto1, 4 => $leafDto4
$authorDto2 = new AuthorDTO(2, "Bob Schmo", [
1 => $bookDto1, 4 => $bookDto4
]);
$rootDto5 = new ValidRootDTO(5, "Root 5", []);
$handmadeResult = [1 => $rootDto1, 2 => $rootDto2, 5 => $rootDto5];
$authorDto5 = new AuthorDTO(5, "Charlie Doe", []);
$handmadeResult = [1 => $authorDto1, 2 => $authorDto2, 5 => $authorDto5];

$this->assertSame(
var_export($flatMapperResults, true),
Expand Down