-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
IterableMapperInterface
for mapping iterables (#53)
- Loading branch information
Showing
10 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
|
||
interface IterableMapperInterface | ||
{ | ||
/** | ||
* @template T of object | ||
* @param iterable<mixed> $source | ||
* @param class-string<T> $target | ||
* @return iterable<T> | ||
*/ | ||
public function mapIterable(iterable $source, string $target, ?Context $context = null): iterable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Common; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\IterableMapperInterface; | ||
|
||
final class IterableMapperDecorator implements IterableMapperInterface | ||
{ | ||
public function __construct( | ||
private IterableMapperInterface $decorated, | ||
private Context $defaultContext | ||
) { | ||
} | ||
|
||
public function mapIterable(iterable $source, string $target, ?Context $context = null): iterable | ||
{ | ||
return $this->decorated->mapIterable($source, $target, $context ?? $this->defaultContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\Basic; | ||
|
||
class Person | ||
{ | ||
public function __construct( | ||
private string $name, | ||
private int $age | ||
) { | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getAge(): int | ||
{ | ||
return $this->age; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\Basic; | ||
|
||
class PersonDto | ||
{ | ||
public ?string $name = null; | ||
public ?int $age = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\IntegrationTest; | ||
|
||
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; | ||
use Rekalogika\Mapper\Tests\Fixtures\Basic\Person; | ||
use Rekalogika\Mapper\Tests\Fixtures\Basic\PersonDto; | ||
|
||
class IterableMapperTest extends FrameworkTestCase | ||
{ | ||
public function testAdder(): void | ||
{ | ||
$result = $this->iterableMapper->mapIterable($this->getIterableInput(), PersonDto::class); | ||
/** @psalm-suppress InvalidArgument */ | ||
$result = iterator_to_array($result); | ||
|
||
$this->assertCount(3, $result); | ||
$this->assertInstanceOf(PersonDto::class, $result[0]); | ||
$this->assertInstanceOf(PersonDto::class, $result[1]); | ||
$this->assertInstanceOf(PersonDto::class, $result[2]); | ||
$this->assertSame('John Doe', $result[0]->name); | ||
$this->assertSame(30, $result[0]->age); | ||
$this->assertSame('Jane Doe', $result[1]->name); | ||
$this->assertSame(25, $result[1]->age); | ||
$this->assertSame('Foo Bar', $result[2]->name); | ||
$this->assertSame(99, $result[2]->age); | ||
} | ||
|
||
/** | ||
* @return iterable<Person> | ||
*/ | ||
private function getIterableInput(): iterable | ||
{ | ||
yield new Person('John Doe', 30); | ||
yield new Person('Jane Doe', 25); | ||
yield new Person('Foo Bar', 99); | ||
} | ||
} |