-
Notifications
You must be signed in to change notification settings - Fork 3
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
0 parents
commit d9fb535
Showing
10 changed files
with
359 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
composer.lock |
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,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; | ||
} | ||
} |
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,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']) | ||
] | ||
) | ||
); | ||
} | ||
} | ||
|
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,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; | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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']; | ||
} | ||
} |
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,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' |
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 @@ | ||
{ | ||
"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]" | ||
} | ||
] | ||
} |