Skip to content

Commit

Permalink
feat: add IterableMapperInterface getter to the non-framework facto…
Browse files Browse the repository at this point in the history
…ry (#54)

* feat: add `IterableMapperInterface` getter to the non-framework factory

* add docs
  • Loading branch information
priyadi committed Apr 29, 2024
1 parent c30d661 commit 5782b3a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.2.0

* feat: add `IterableMapperInterface` for mapping iterables
* feat: add `IterableMapperInterface` getter to the non-framework factory

## 1.1.2

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ composer require rekalogika/mapper
```php
use App\Entity\Book;
use Rekalogika\Mapper\MapperInterface;
use Rekalogika\Mapper\IterableMapperInterface;

/** @var MapperInterface $mapper */
/** @var IterableMapperInterface $mapper */
/** @var Book $book */
/** @var iterable<Book> $books */

$result = $mapper->map($book, BookDto::class);
$bookDto = $mapper->map($book, BookDto::class);

// or map to an existing object

$bookDto = new BookDto();
$mapper->map($book, $bookDto);

// map iterable of books

$bookDtos = $mapper->mapIterable($books, BookDto::class);
```

## Why Use a Mapper?
Expand Down
10 changes: 10 additions & 0 deletions src/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class MapperFactory
private ?ArrayLikeMetadataFactoryInterface $arrayLikeMetadataFactory = null;
private ?MainTransformerInterface $mainTransformer = null;
private ?MapperInterface $mapper = null;
private ?IterableMapperInterface $iterableMapper = null;
private ?MappingFactoryInterface $mappingFactory = null;
private ?ObjectCacheFactoryInterface $objectCacheFactory = null;
private ?SubMapperFactoryInterface $subMapperFactory = null;
Expand Down Expand Up @@ -227,6 +228,15 @@ public function getMapper(): MapperInterface
return $this->mapper;
}

public function getIterableMapper(): IterableMapperInterface
{
if (null === $this->iterableMapper) {
$this->iterableMapper = new Mapper($this->getMainTransformer());
}

return $this->iterableMapper;
}

//
// property info
//
Expand Down

0 comments on commit 5782b3a

Please sign in to comment.