-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
15 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,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\Money; | ||
|
||
class MoneyDto | ||
{ | ||
public function __construct( | ||
private string $amount, | ||
private string $currency, | ||
) { | ||
} | ||
|
||
public function getAmount(): string | ||
{ | ||
return $this->amount; | ||
} | ||
|
||
public function getCurrency(): string | ||
{ | ||
return $this->currency; | ||
} | ||
} |
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,89 @@ | ||
<?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\Transformer; | ||
|
||
use Brick\Money\Money; | ||
use Rekalogika\Mapper\Contracts\TransformerInterface; | ||
use Rekalogika\Mapper\Contracts\TypeMapping; | ||
use Rekalogika\Mapper\Exception\InvalidArgumentException; | ||
use Rekalogika\Mapper\Tests\Fixtures\Money\MoneyDto; | ||
use Rekalogika\Mapper\Util\TypeCheck; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class MoneyToMoneyDtoTransformer implements TransformerInterface | ||
{ | ||
// This tells the library that this transformer supports the transformation | ||
// from the Money object to the MoneyDto object, and vice versa. | ||
// | ||
// The TypeFactory methods are convenience methods for creating the | ||
// PropertyInfo Type objects. | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
|
||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(Money::class), | ||
TypeFactory::objectOfClass(MoneyDto::class) | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(MoneyDto::class), | ||
TypeFactory::objectOfClass(Money::class) | ||
); | ||
} | ||
|
||
// This method is called when the mapper is trying to transform Money to | ||
// MoneyDto, and vice versa. | ||
// | ||
// The $source and $target parameters are the source and target objects, | ||
// respectively. $target is usually null, unless there is already an | ||
// existing value in the target object. | ||
// | ||
// $sourceType and $targetType are the types of the source and target, in | ||
// the form of PropertyInfo Type object. | ||
// | ||
// The TypeCheck class is a convenience class for verifying the type | ||
// specified by a Type object. | ||
|
||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
Type $sourceType, | ||
Type $targetType, | ||
array $context | ||
): mixed { | ||
if ( | ||
$source instanceof Money | ||
&& TypeCheck::isObjectOfType($targetType, MoneyDto::class) | ||
) { | ||
return new MoneyDto( | ||
amount: $source->getAmount()->__toString(), | ||
currency: $source->getCurrency()->getCurrencyCode(), | ||
); | ||
} | ||
|
||
if ( | ||
$source instanceof MoneyDto | ||
&& TypeCheck::isObjectOfType($targetType, Money::class) | ||
) { | ||
return Money::of( | ||
$source->getAmount(), | ||
$source->getCurrency() | ||
); | ||
} | ||
|
||
throw new InvalidArgumentException('Unsupported transformation'); | ||
} | ||
} |
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,47 @@ | ||
<?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 Brick\Money\Money; | ||
use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest; | ||
use Rekalogika\Mapper\Tests\Fixtures\Money\MoneyDto; | ||
use Rekalogika\Mapper\Tests\Fixtures\Transformer\MoneyToMoneyDtoTransformer; | ||
|
||
class CustomTransformerTest extends AbstractIntegrationTest | ||
{ | ||
protected function getAdditionalTransformers(): array | ||
{ | ||
return [ | ||
'MoneyToMoneyDtoTransformer' => new MoneyToMoneyDtoTransformer(), | ||
]; | ||
} | ||
|
||
public function testMoneyToMoneyDto(): void | ||
{ | ||
$money = Money::of("100000.00", 'IDR'); | ||
$moneyDto = $this->mapper->map($money, MoneyDto::class); | ||
|
||
$this->assertSame('100000.00', $moneyDto->getAmount()); | ||
$this->assertSame('IDR', $moneyDto->getCurrency()); | ||
} | ||
|
||
public function testMoneyDtoToMoney(): void | ||
{ | ||
$moneyDto = new MoneyDto('100000.00', 'IDR'); | ||
$money = $this->mapper->map($moneyDto, Money::class); | ||
|
||
$this->assertSame('100000.00', $money->getAmount()->__toString()); | ||
$this->assertSame('IDR', $money->getCurrency()->getCurrencyCode()); | ||
} | ||
} |