Skip to content

Commit

Permalink
Replace money instances with BigInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Apr 20, 2024
1 parent f197f7f commit 7d2708a
Show file tree
Hide file tree
Showing 74 changed files with 990 additions and 1,005 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ext-xsl": "*",
"api-platform/core": "^2.6",
"async-aws/ses": "^1.3",
"brick/math": "^0.9 || ^0.10.2",
"brick/math": "^0.11.0",
"composer/package-versions-deprecated": "^1.8",
"defuse/php-encryption": "^2.2",
"doctrine/cache": "^2.2",
Expand Down
19 changes: 9 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/packages/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Ramsey\Uuid\Doctrine\UuidType;
use SolidInvoice\CoreBundle\Doctrine\Filter\ArchivableFilter;
use SolidInvoice\CoreBundle\Doctrine\Filter\CompanyFilter;
use SolidInvoice\CoreBundle\Doctrine\Type\BigIntegerType;
use SolidInvoice\MoneyBundle\Doctrine\Hydrator\MoneyHydrator;
use Symfony\Config\DoctrineConfig;
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
Expand Down Expand Up @@ -36,6 +37,10 @@
->type(UuidBinaryOrderedTimeType::NAME)
->class(UuidBinaryOrderedTimeType::class);

$dbalConfig
->type(BigIntegerType::NAME)
->class(BigIntegerType::class);

$ormConfig->autoGenerateProxyClasses(param('kernel.debug'));

$entityManagerConfig = $ormConfig->entityManager('default');
Expand Down
55 changes: 55 additions & 0 deletions src/ApiBundle/Serializer/Normalizer/BigIntegerNormalizer.php
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;
}
}
4 changes: 2 additions & 2 deletions src/ApiBundle/Serializer/Normalizer/DiscountNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace SolidInvoice\ApiBundle\Serializer\Normalizer;

use Money\Money;
use Brick\Math\BigInteger;
use SolidInvoice\CoreBundle\Entity\Discount;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function supportsDenormalization($data, $type, $format = null): bool
/**
* @param Discount $object
* @param array<string, mixed> $context
* @return array{type: string, value: float|Money|null}
* @return array{type: string, value: BigInteger|int|float|string|null}
*/
public function normalize($object, string $format = null, array $context = []): array
{
Expand Down
69 changes: 0 additions & 69 deletions src/ApiBundle/Serializer/Normalizer/MoneyNormalizer.php

This file was deleted.

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

namespace SolidInvoice\ApiBundle\Tests\Serializer\Normalizer;

use Money\Currency;
use Money\Money;
use Brick\Math\BigInteger;
use Brick\Math\Exception\MathException;
use PHPUnit\Framework\TestCase;
use SolidInvoice\ApiBundle\Serializer\Normalizer\CreditNormalizer;
use SolidInvoice\ClientBundle\Entity\Credit;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand Down Expand Up @@ -83,6 +84,10 @@ public function denormalize($data, $class, $format = null, array $context = [])
self::assertFalse($normalizer->supportsDenormalization([], NormalizerInterface::class));
}

/**
* @throws MathException
* @throws ExceptionInterface
*/
public function testNormalization(): void
{
$parentNormalizer = new class() implements NormalizerInterface, DenormalizerInterface {
Expand Down Expand Up @@ -110,10 +115,9 @@ public function denormalize($data, $class, $format = null, array $context = [])
$normalizer = new CreditNormalizer($parentNormalizer);

$credit = new Credit();
$money = new Money(10000, new Currency('USD'));
$credit->setValue($money);
$credit->setValue(10000);

self::assertEquals($money, $normalizer->normalize($credit));
self::assertEquals(BigInteger::of(10000), $normalizer->normalize($credit));
}

public function testDenormalization(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace SolidInvoice\ApiBundle\Tests\Serializer\Normalizer;

use Money\Currency;
use Money\Money;
use Brick\Math\BigInteger;
use Brick\Math\Exception\MathException;
use PHPUnit\Framework\TestCase;
use SolidInvoice\ApiBundle\Serializer\Normalizer\DiscountNormalizer;
use SolidInvoice\CoreBundle\Entity\Discount;
Expand All @@ -41,13 +41,16 @@ public function testSupportsDenormalization(): void
self::assertFalse($this->normalizer->supportsDenormalization([], NormalizerInterface::class));
}

/**
* @throws MathException
*/
public function testNormalization(): void
{
$discount = new Discount();
$discount->setType(Discount::TYPE_MONEY);
$discount->setValue(100);

self::assertEquals(['type' => 'money', 'value' => new Money(10000, new Currency('USD'))], $this->normalizer->normalize($discount));
self::assertEquals(['type' => 'money', 'value' => BigInteger::of(100)], $this->normalizer->normalize($discount));
}

public function testDenormalization(): void
Expand Down

0 comments on commit 7d2708a

Please sign in to comment.