Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored to use brick/money #4

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('padam87_money');

Expand All @@ -31,6 +31,9 @@ public function getConfigTreeBuilder()
->scalarPrototype()->end()
->defaultValue(['EUR'])
->end()
->arrayNode('currency_digits')
->scalarPrototype()->end()
->end()
->end()
;

Expand Down
40 changes: 12 additions & 28 deletions DependencyInjection/Padam87MoneyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Padam87\MoneyBundle\DependencyInjection;

use Money\Currencies;
use Money\Currencies\CurrencyList;
use Padam87\MoneyBundle\Doctrine\Mapping\Driver\MoneyEmbeddedDriver;
use Padam87\MoneyBundle\Doctrine\Type\CurrencyType;
use Padam87\MoneyBundle\Doctrine\Type\MoneyAmountType;
use Padam87\MoneyBundle\Doctrine\Type\DecimalObjectType;
use Padam87\MoneyBundle\Money\EmbeddedMoney;
use Padam87\MoneyBundle\Money\NullableMoney;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
Expand All @@ -19,7 +19,7 @@ class Padam87MoneyExtension extends Extension implements PrependExtensionInterfa
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand All @@ -33,14 +33,14 @@ public function load(array $configs, ContainerBuilder $container)
/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
public function prepend(ContainerBuilder $container): void
{
$container->prependExtensionConfig(
'doctrine',
[
'dbal' => [
'types' => [
'money_amount' => MoneyAmountType::class,
'decimal_object' => DecimalObjectType::class,
'currency' => CurrencyType::class,
]
],
Expand All @@ -51,39 +51,23 @@ public function prepend(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$config = $container->getParameter('padam87_money.config');

$driver = $container->getDefinition('doctrine.orm.default_metadata_driver');

$driver->addMethodCall(
'addDriver',
[
$container->getDefinition('Padam87\MoneyBundle\Doctrine\Mapping\Driver\MoneyEmbeddedDriver'),
'Money\Money'
$container->getDefinition(MoneyEmbeddedDriver::class),
EmbeddedMoney::class
]
);

$driver->addMethodCall(
'addDriver',
[
$container->getDefinition('Padam87\MoneyBundle\Doctrine\Mapping\Driver\CurrencyPairEmbeddedDriver'),
'Money\CurrencyPair'
$container->getDefinition(MoneyEmbeddedDriver::class),
NullableMoney::class
]
);

$container->setDefinition(
CurrencyList::class,
new Definition(
CurrencyList::class,
[
array_fill_keys($config['currencies'], $config['scale'])
]
)
);

$container->setAlias(Currencies::class, CurrencyList::class);
}
}

59 changes: 0 additions & 59 deletions Doctrine/Mapping/Driver/CurrencyPairEmbeddedDriver.php

This file was deleted.

18 changes: 11 additions & 7 deletions Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Money\Money;
use Padam87\MoneyBundle\Money\EmbeddedMoney;
use Padam87\MoneyBundle\Money\NullableMoney;

class MoneyEmbeddedDriver implements MappingDriver
{
private $config;
private array $config;

public function __construct(array $config)
{
Expand All @@ -18,7 +19,7 @@ public function __construct(array $config)
/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
public function loadMetadataForClass($className, ClassMetadata $metadata): void
{
/* @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */

Expand All @@ -27,34 +28,37 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$metadata->mapField(
[
'fieldName' => 'amount',
'type' => 'money_amount',
'type' => 'decimal_object',
'precision' => $this->config['precision'],
'scale' => $this->config['scale'],
'nullable' => $className === NullableMoney::class,
]
);

$metadata->mapField(
[
'fieldName' => 'currency',
'type' => 'currency',
'nullable' => $className === NullableMoney::class,
]
);
}

/**
* {@inheritdoc}
*/
public function getAllClassNames()
public function getAllClassNames(): array
{
return [
Money::class
EmbeddedMoney::class,
NullableMoney::class,
];
}

/**
* {@inheritdoc}
*/
public function isTransient($className)
public function isTransient($className): bool
{
return false;
}
Expand Down
34 changes: 23 additions & 11 deletions Doctrine/Type/CurrencyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,71 @@

namespace Padam87\MoneyBundle\Doctrine\Type;

use Brick\Money\Currency;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Money\Currency;

class CurrencyType extends Type
{
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'currency';
}

/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
$fieldDeclaration['length'] = 3;
$column['length'] = $this->getDefaultLength($platform); // enforce column length even if specified

return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
return $platform->getStringTypeDeclarationSQL($column);
}

/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
public function getDefaultLength(AbstractPlatform $platform): int
{
return 3;
}

/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform): ?Currency
{
if (null === $value) {
return null;
}

return new Currency($value);
return Currency::of($value);
}

/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}

if (!$value instanceof Currency) {
throw new \LogicException();
throw new \LogicException(sprintf('Only instances of "%s" can be persisted as currency', Currency::class));
}

return $value->getCode();
return (string) $value->getCurrencyCode();
}
}
54 changes: 54 additions & 0 deletions Doctrine/Type/DecimalObjectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Padam87\MoneyBundle\Doctrine\Type;

use Brick\Math\BigDecimal;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DecimalType;

class DecimalObjectType extends DecimalType
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'decimal_object';
}

/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?BigDecimal
{
if (null === $value) {
return null;
}

return BigDecimal::of($value);
}

/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}

if (!$value instanceof BigDecimal) {
throw new \LogicException(sprintf('Only instances of "%s" can be persisted as decimal', BigDecimal::class));
}

return (string) $value;
}
}
Loading