-
-
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
9 changed files
with
251 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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\Issue232; | ||
|
||
final readonly class BigDecimalSource | ||
{ | ||
public function __construct( | ||
public string $amount, | ||
) {} | ||
} |
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,23 @@ | ||
<?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\Issue232; | ||
|
||
use Brick\Math\BigDecimal; | ||
|
||
final readonly class BigDecimalTarget | ||
{ | ||
public function __construct( | ||
public BigDecimal $amount, | ||
) {} | ||
} |
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,21 @@ | ||
<?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\Issue232; | ||
|
||
final readonly class CurrencySource | ||
{ | ||
public function __construct( | ||
public string $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,23 @@ | ||
<?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\Issue232; | ||
|
||
use Brick\Money\Currency; | ||
|
||
final readonly class CurrencyTarget | ||
{ | ||
public function __construct( | ||
public Currency $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,39 @@ | ||
<?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\Issue232\BigDecimalSource; | ||
use Rekalogika\Mapper\Tests\Fixtures\Issue232\BigDecimalTarget; | ||
use Rekalogika\Mapper\Tests\Fixtures\Issue232\CurrencySource; | ||
use Rekalogika\Mapper\Tests\Fixtures\Issue232\CurrencyTarget; | ||
|
||
class Issue232Test extends FrameworkTestCase | ||
{ | ||
public function testBigDecimal(): void | ||
{ | ||
$source = new BigDecimalSource('100'); | ||
$target = $this->mapper->map($source, BigDecimalTarget::class); | ||
|
||
$this->assertEquals('100', $target->amount->toScale(0)->jsonSerialize()); | ||
} | ||
|
||
public function testCurrency(): void | ||
{ | ||
$source = new CurrencySource('USD'); | ||
$target = $this->mapper->map($source, CurrencyTarget::class); | ||
|
||
$this->assertEquals('USD', $target->currency->getCurrencyCode()); | ||
} | ||
} |
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,65 @@ | ||
<?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\Services\Issue232; | ||
|
||
use Brick\Math\BigDecimal; | ||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Transformer\TransformerInterface; | ||
use Rekalogika\Mapper\Transformer\TypeMapping; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final readonly class BigDecimalTransformer implements TransformerInterface | ||
{ | ||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
?Type $sourceType, | ||
?Type $targetType, | ||
Context $context, | ||
): BigDecimal|string { | ||
if ($source instanceof BigDecimal) { | ||
return (string) $source; | ||
} | ||
|
||
/** | ||
* @psalm-suppress MixedArgument | ||
* @phpstan-ignore argument.type | ||
*/ | ||
return BigDecimal::of($source)->toBigDecimal(); | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
yield new TypeMapping( | ||
TypeFactory::string(), | ||
TypeFactory::objectOfClass(BigDecimal::class), | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::int(), | ||
TypeFactory::objectOfClass(BigDecimal::class), | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::float(), | ||
TypeFactory::objectOfClass(BigDecimal::class), | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(BigDecimal::class), | ||
TypeFactory::string(), | ||
); | ||
} | ||
} |
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,46 @@ | ||
<?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\Services\Issue232; | ||
|
||
use Brick\Money\Currency; | ||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Transformer\TransformerInterface; | ||
use Rekalogika\Mapper\Transformer\TypeMapping; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final readonly class CurrencyTransformer implements TransformerInterface | ||
{ | ||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
?Type $sourceType, | ||
?Type $targetType, | ||
Context $context, | ||
): mixed { | ||
/** | ||
* @psalm-suppress MixedArgument | ||
* @phpstan-ignore argument.type | ||
*/ | ||
return Currency::of($source); | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
yield new TypeMapping( | ||
TypeFactory::string(), | ||
TypeFactory::objectOfClass(Currency::class), | ||
); | ||
} | ||
} |