diff --git a/CHANGELOG.md b/CHANGELOG.md index d48032c..ae340d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 298529e..cf274aa 100644 --- a/README.md +++ b/README.md @@ -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 $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? diff --git a/src/MapperFactory.php b/src/MapperFactory.php index dac4f91..152a8fc 100644 --- a/src/MapperFactory.php +++ b/src/MapperFactory.php @@ -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; @@ -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 //