Skip to content

Commit 3a6627a

Browse files
authored
docs: add documentation for mapping collections feature (#248)
fix #241
2 parents dfe60b0 + 9854710 commit 3a6627a

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

docs/_nav.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Mapping](mapping/index.md)
88
- [MapTo and MapFrom attributes](mapping/attributes.md)
99
- [Symfony Serializer](mapping/serializer.md)
10+
- [Mapping Collections](mapping/map-collection.md)
1011
- [Ignoring properties](mapping/ignoring-properties.md)
1112
- [Conditional mapping](mapping/conditional-mapping.md)
1213
- [Groups](mapping/groups.md)

docs/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ $target = $automapper->map($source, Target::class);
2626
That's it! `AutoMapper` will find the best way to map from `$source` object to a new `Target` object. It will also
2727
generate a PHP class that will do the mapping for you and serve as a cache for future mapping.
2828

29+
### Mapping Collections
30+
31+
AutoMapper can also map collections of objects with the `mapCollection` method:
32+
33+
```php
34+
$sources = [$source1, $source2, $source3];
35+
$targets = $automapper->mapCollection($sources, Target::class);
36+
```
37+
2938
Of course there are many ways to customize the mapping, this documentation will explain all of them.
3039

3140
## Installation 📦

docs/mapping/map-collection.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Mapping Collections
2+
3+
When working with AutoMapper, you often need to map not just a single object, but a collection of objects. The `mapCollection()` method is designed specifically for this purpose.
4+
5+
## Basic Usage
6+
7+
The `mapCollection()` method allows you to transform a collection of objects from one type to another:
8+
9+
```php
10+
use AutoMapper\AutoMapper;
11+
12+
$automapper = AutoMapper::create();
13+
14+
$sources = [
15+
new Source('value1'),
16+
new Source('value2'),
17+
new Source('value3'),
18+
];
19+
20+
$targets = $automapper->mapCollection($sources, Target::class);
21+
```
22+
23+
This will return an array where each element from the source collection is mapped to a new instance of the target class.
24+
25+
## Collection Types Support
26+
27+
AutoMapper's `mapCollection()` supports any `iterable`
28+
29+
The method will return an array of object that was provided as a target class:
30+
31+
```php
32+
$targetsArray = $automapper->mapCollection($sourcesArray, Target::class);
33+
```
34+
35+
## Using Context
36+
37+
Just like the `map()` method, `mapCollection()` also accepts a context parameter for customizing the mapping process:
38+
39+
```php
40+
$context = ['groups' => ['read']];
41+
$targets = $automapper->mapCollection($sources, Target::class, $context);
42+
```
43+
44+
This context will be applied to each individual object mapping operation.

0 commit comments

Comments
 (0)