-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace money instances with BigInteger
- Loading branch information
Showing
74 changed files
with
990 additions
and
1,005 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
src/ApiBundle/Serializer/Normalizer/BigIntegerNormalizer.php
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of SolidInvoice project. | ||
* | ||
* (c) Pierre du Plessis <open-source@solidworx.co> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace SolidInvoice\ApiBundle\Serializer\Normalizer; | ||
|
||
use Brick\Math\BigNumber; | ||
use Brick\Math\Exception\MathException; | ||
use Brick\Math\RoundingMode; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use function is_a; | ||
|
||
/** | ||
* @see \SolidInvoice\ApiBundle\Tests\Serializer\Normalizer\BigIntegerNormalizerTest | ||
*/ | ||
class BigIntegerNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
/** | ||
* @throws MathException | ||
*/ | ||
public function denormalize($data, string $type, string $format = null, array $context = []): BigNumber | ||
{ | ||
return BigNumber::of($data)->toBigDecimal()->multipliedBy(100)->toBigInteger(); | ||
} | ||
|
||
public function supportsDenormalization($data, $type, $format = null): bool | ||
{ | ||
return is_a($type, BigNumber::class, true); | ||
} | ||
|
||
/** | ||
* @param BigNumber $object | ||
* @param array<string, mixed> $context | ||
* @throws MathException | ||
*/ | ||
public function normalize($object, string $format = null, array $context = []): float | ||
{ | ||
return $object->toBigDecimal()->dividedBy(100, 2, RoundingMode::HALF_EVEN)->toFloat(); | ||
} | ||
|
||
public function supportsNormalization($data, $format = null): bool | ||
{ | ||
return $data instanceof BigNumber; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
src/ApiBundle/Tests/Serializer/Normalizer/BigIntegerNormalizerTest.php
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of SolidInvoice project. | ||
* | ||
* (c) Pierre du Plessis <open-source@solidworx.co> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace SolidInvoice\ApiBundle\Tests\Serializer\Normalizer; | ||
|
||
use Brick\Math\BigDecimal; | ||
use Brick\Math\BigInteger; | ||
use Brick\Math\BigNumber; | ||
use Brick\Math\Exception\MathException; | ||
use PHPUnit\Framework\TestCase; | ||
use SolidInvoice\ApiBundle\Serializer\Normalizer\BigIntegerNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class BigIntegerNormalizerTest extends TestCase | ||
{ | ||
private BigIntegerNormalizer $normalizer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->normalizer = new BigIntegerNormalizer(); | ||
} | ||
|
||
/** | ||
* @throws MathException | ||
*/ | ||
public function testSupportsNormalization(): void | ||
{ | ||
self::assertTrue($this->normalizer->supportsNormalization(BigInteger::of(1))); | ||
self::assertTrue($this->normalizer->supportsNormalization(BigDecimal::of(1.1))); | ||
self::assertTrue($this->normalizer->supportsNormalization(BigNumber::of(1.1))); | ||
self::assertFalse($this->normalizer->supportsNormalization(BigInteger::class)); | ||
} | ||
|
||
public function testSupportsDenormalization(): void | ||
{ | ||
self::assertTrue($this->normalizer->supportsDenormalization(null, BigInteger::class)); | ||
self::assertTrue($this->normalizer->supportsDenormalization(null, BigDecimal::class)); | ||
self::assertTrue($this->normalizer->supportsDenormalization(null, BigNumber::class)); | ||
self::assertFalse($this->normalizer->supportsDenormalization([], NormalizerInterface::class)); | ||
} | ||
|
||
/** | ||
* @throws MathException | ||
*/ | ||
public function testNormalization(): void | ||
{ | ||
self::assertEquals(1, $this->normalizer->normalize(BigInteger::of(100))); | ||
} | ||
|
||
/** | ||
* @throws MathException | ||
*/ | ||
public function testDenormalization(): void | ||
{ | ||
self::assertEquals(BigInteger::of(1000000), $this->normalizer->denormalize(10000, BigNumber::class)); | ||
self::assertEquals(BigInteger::of(1000010), $this->normalizer->denormalize(10000.1, BigNumber::class)); | ||
} | ||
} |
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
Oops, something went wrong.