Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Padam87 committed Jul 24, 2018
0 parents commit d9fb535
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
37 changes: 37 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Padam87\MoneyBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('padam87_money');

$rootNode
->addDefaultsIfNotSet()
->children()
->integerNode('precision')
->defaultValue(18)
->end()
->integerNode('scale')
->defaultValue(2)
->end()
->arrayNode('currencies')
->scalarPrototype()->end()
->defaultValue(['EUR'])
->end()
->end()
;

return $treeBuilder;
}
}
76 changes: 76 additions & 0 deletions DependencyInjection/Padam87MoneyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Padam87\MoneyBundle\DependencyInjection;

use Money\Currencies\CurrencyList;
use Padam87\MoneyBundle\Doctrine\Type\CurrencyType;
use Padam87\MoneyBundle\Doctrine\Type\MoneyAmountType;
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;

class Padam87MoneyExtension extends Extension implements PrependExtensionInterface, CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

$container->setParameter('padam87_money.config', $config);
}

/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig(
'doctrine',
[
'dbal' => [
'types' => [
'money_amount' => MoneyAmountType::class,
'currency' => CurrencyType::class,
]
],
]
);
}

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$config = $container->getParameter('padam87_money.config');

$container->getDefinition('doctrine.orm.default_metadata_driver')->addMethodCall(
'addDriver',
[
$container->getDefinition('Padam87\MoneyBundle\Doctrine\Mapping\Driver\MoneyEmbeddedDriver'),
'Money'
]
);

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

61 changes: 61 additions & 0 deletions Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Padam87\MoneyBundle\Doctrine\Mapping\Driver;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Money\Money;

class MoneyEmbeddedDriver implements MappingDriver
{
private $config;

public function __construct(array $config)
{
$this->config = $config;
}

/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
{
/* @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */

$metadata->isEmbeddedClass = true;

$metadata->mapField(
[
'fieldName' => 'amount',
'type' => 'money_amount',
'precision' => $this->config['precision'],
'scale' => $this->config['scale'],
]
);

$metadata->mapField(
[
'fieldName' => 'currency',
'type' => 'currency',
]
);
}

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

/**
* {@inheritdoc}
*/
public function isTransient($className)
{
return false;
}
}
60 changes: 60 additions & 0 deletions Doctrine/Type/CurrencyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Padam87\MoneyBundle\Doctrine\Type;

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

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

/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$fieldDeclaration['length'] = 3;

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

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

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

return new Currency($value);
}

/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (!$value instanceof Currency) {
throw new \LogicException();
}

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

namespace Padam87\MoneyBundle\Doctrine\Type;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DecimalType;

class MoneyAmountType extends DecimalType
{
/**
* This value is set by the configuration
*
* @var int
*/
static $precision;

/**
* This value is set by the configuration
*
* @var int
*/
static $scale;

/**
* {@inheritdoc}
*/
public function getName()
{
return 'money_amount';
}

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

/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return bcmul($value, pow(10, self::$scale), 0);
}
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return bcdiv($value, pow(10, self::$scale), self::$scale);
}
}
13 changes: 13 additions & 0 deletions Money/Formatter/NumberFormatterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Padam87\MoneyBundle\Money\Formatter;

use Symfony\Component\HttpFoundation\RequestStack;

class NumberFormatterFactory
{
public static function createNumberFormatter(RequestStack $requestStack)
{
return new \NumberFormatter($requestStack->getCurrentRequest()->getLocale(), \NumberFormatter::CURRENCY);
}
}
17 changes: 17 additions & 0 deletions Padam87MoneyBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Padam87\MoneyBundle;

use Padam87\MoneyBundle\Doctrine\Type\MoneyAmountType;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class Padam87MoneyBundle extends Bundle
{
public function boot()
{
$config = $this->container->getParameter('padam87_money.config');

MoneyAmountType::$precision = $config['precision'];
MoneyAmountType::$scale = $config['scale'];
}
}
16 changes: 16 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
Padam87\MoneyBundle\Doctrine\Mapping\Driver\MoneyEmbeddedDriver:
arguments:
$config: '%padam87_money.config%'

Padam87\MoneyBundle\Money\Formatter\NumberFormatterFactory: ~

padam87_money.number_formatter:
class: \NumberFormatter
factory: 'Padam87\MoneyBundle\Money\Formatter\NumberFormatterFactory:createNumberFormatter'
arguments: ['@request_stack']

Money\Formatter\IntlMoneyFormatter:
arguments:
- '@padam87_money.number_formatter'
- '@Money\Currencies\CurrencyList'
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "padam87/money-bundle",
"description": "Symfony bundle for https://github.com/moneyphp/money",
"type": "symfony-bundle",
"require": {
"php": "^7.0",
"ext-bcmath": "^7.0",
"symfony/framework-bundle": "^3.4",
"moneyphp/money": "^3.1",
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^7.2"
},
"license": "MIT",
"authors": [
{
"name": "Adam Prager",
"email": "[email protected]"
}
]
}

0 comments on commit d9fb535

Please sign in to comment.