From 5782b3ab0a2c0b93ddef298f2866de6ae331f76e Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:47:40 +0700 Subject: [PATCH] feat: add `IterableMapperInterface` getter to the non-framework factory (#54) * feat: add `IterableMapperInterface` getter to the non-framework factory * add docs --- CHANGELOG.md | 1 + README.md | 9 ++++++++- src/MapperFactory.php | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) 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 //