Download the PHP package enesdayanc/money-bundle without Composer
On this page you can find all versions of the php package enesdayanc/money-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download enesdayanc/money-bundle
More information about enesdayanc/money-bundle
Files in enesdayanc/money-bundle
Package money-bundle
Short Description This is a Symfony bundle that integrates moneyphp/money library (Fowler pattern): https://github.com/moneyphp/money.
License MIT
Homepage https://github.com/TheBigBrainsCompany/TbbcMoneyBundle
Informations about the package money-bundle
TbbcMoneyBundle
This bundle is used to integrate the Money library from mathiasverraes into a Symfony project.
This library is based on Fowler's Money pattern
- This bundle is tested and is stable with Symfony 3.4, 4.3, 4.4, 5.0
Quick Start
Features
- Integrates money library from mathiasverraes
- Twig filters and PHP helpers for helping with money and currencies in templates
- A storage system for currency ratios
- A ratioProvider system for fetching ratio from externals api
- Symfony form integration
- Console commands for different operations
- A configuration parser for specifying website used currencies
- Access to the history of currency ratio fetched
- Money formatter i18n
Table of contents
- Installation
- Usage
- Storage
- Contributing
- Requirements
- Authors
- Status
Installation
Use Composer and install with
$ composer require tbbc/money-bundle
If you use Symfony 3 then add the bundle to AppKernel:
For Symfony 4 and higher add the bundle to config/bundles.php (if it was not automatically added during the installation of the package):
For Symfony 3, in your config.yml, add the currencies you want to use and the reference currency. For Symfony 4 and higher create a file like config/packages/tbbc_money.yml and add it there.
In your config.yml or config/packages/tbbc_money.yml, add the form fields presentations
You should also register custom Doctrine Money type:
Usage
Money Library integration
Form integration
You have 3 new form types (under Tbbc\MoneyBundle\Form\Type namespace):
- CurrencyType : asks for a currency among currencies defined in config.yml
- MoneyType : asks for an amount and a currency
- SimpleMoneyType : asks for an amount and sets the currency to the reference currency set in config.yml
Example :
Manipulating the form
With MoneyType
you can manipulate the form elements with
amount_options
for the amount field, and currency_options
for the currency field, fx if you want to change the label.
With CurrencyType
only currency_options
can be used, and with SimpleMoneyType
only amount_options
can be used.
Saving Money with Doctrine
Solution 1 : two fields in the database
Note that there are 2 columns in the DB table : $priceAmount and $priceCurrency and only one getter/setter : getPrice and setPrice.
The get/setPrice methods are dealing with these two columns transparently.
- Advantage : your DB is clean and you can do sql sum, group by, sort,... with the amount and the currency in two different columns in your db
- Disadvantage : it is ugly in the entity.
Solution 2 : use Doctrine type
There is only one string column in your DB table. The money object is manually serialized by the new Doctrine type.
1.25€ is serialized in your DB by 'EUR 125'. This format is stable. It won't change in future releases..
The new Doctrine type name is "money".
- Advantage : The entity is easy to create and use
- Disadvantage : it is more difficult to directly request the db in SQL.
Conversion manager
Convert an amount into another currency
Save a conversion value in a DB
Money formatter
Twig integration
PHP templating integration
Fetching ratio values from remote provider
Change the ratio provider
The ratio provider by default is base on the service 'tbbc_money.ratio_provider.ecb'
This bundles contains three ratio providers :
- tbbc_money.ratio_provider.ecb based on the data provided here https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
- tbbc_money.ratio_provider.yahoo_finance based on the Yahoo finance APIs https://developer.yahoo.com/ (yahoo does not provide this api anymore)
- tbbc_money.ratio_provider.google based on the https://www.google.com/finance/converter service (google does not provide this api anymore)
You can change the service to use in the config.yml file :
Additional rate providers from Exchanger
This project integrates https://github.com/florianv/exchanger library to work with currency exchange rates from various services.
Installation:
composer require "florianv/exchanger" "php-http/message" "php-http/guzzle6-adapter"
Configuration:
First, you need to add services you would like to use into your services.yml file, e.g:
Second, you need to update ratio provider used by MoneyBundle on your config.yml file:
Recommended:
Some providers focus on a limited set of currencies, but give better data. You can use several rate providers seamlessly on your project by bundling them into the chain. If some provider does not support certain currency, next provider in the chain would be attempted.
Example of chained providers:
As you can see here 4 providers would be attempted one after another until conversion rate is found. Check this page for a fill list of supported services and their configurations: https://github.com/florianv/exchanger/blob/master/doc/readme.md#supported-services
And then you need to assign rate provider on your config.yml file:
Create your own ratio provider
A ratio provider is a service that implements the Tbbc\MoneyBundle\Pair\RatioProviderInterface
.
I recommend that you read the PHP doc of the interface to understand how to implement a new ratio provider.
The new ratio provider has to be registered as a service.
To use the new ratio provider, you should set the service to use in the config.yml by giving the service name.
automatic currency ratio fetch
Add to your crontab :
MoneyManager : create a money object from a float
Create a money object from a float can be a bit tricky because of rounding issues.
history of currency ratio with the pairHistoryManager
Doctrine is required to use this feature.
In order to get the ratio history, you have to enable it in the configuration and to use Doctrine.
Then you can use the service :
RatioStorage
Two storages for storing ratios are available : CSV File, or Doctrine By default, TbbcMoneyBundle is configured with CSV File.
If you want to switch to a Doctrine storage, edit your config.yml
Update your database schema :
With the Doctrine storage, currency ratio will use the default entity manager and will store data inside the tbbc_money_doctrine_storage_ratios
Custom NumberFormatter in MoneyFormatter
The MoneyFormatter::localizedFormatMoney ( service 'tbbc_money.formatter.money_formatter' ) use the php NumberFormatter class ( http://www.php.net/manual/en/numberformatter.formatcurrency.php ) to format money.
You can :
- give your own \NumberFormatter instance as a parameter of MoneyFormatter::localizedFormatMoney
- subclass the MoneyFormatter and rewrite the getDefaultNumberFormater method to set a application wide NumberFormatter
Using the TbbcMoneyBundle without Doctrine
You have to disable the pair history service in order to use the TbbcMoneyBundle without Doctrine.
Note : you can imagine to code your own PairHistoryManager for MongoDB or Propel, it is very easy to do. Don't hesitate to submit a PR with your code and your tests.
Optimizations
In your config.yml, you can :
- select the templating engine to use. By default, only Twig is loaded.
- define the decimals count after a unit (ex : 12.25€ : 2 decimals ; 11.5678€ : 4 decimals)
Contributing
- Take a look at the list of issues.
- Fork
- Write a test (for either new feature or bug)
- Make a PR
Requirements
- PHP 5.3.9+
- Symfony 2.8+
Authors
Philippe Le Van - kitpages.fr - twitter : @plv
Thomas Tourlourat - Wozbe - twitter: @armetiz
Status
Stable
what is functional:
- integration of the money library
- configuration parser
- pairManager
- Travis CI integration
- form integration
- Twig presentation for forms
- Twig filters
- commands for ratio creation and ratio display
- automatic ratio fetch (with 2 ratio providers)
- history of currency ratio
All versions of money-bundle with dependencies
ext-curl Version *
symfony/framework-bundle Version ^3.4|^4.0|^5.0
moneyphp/money Version ^3.0
symfony/form Version ^3.4|^4.0|^5.0
symfony/twig-bundle Version ^3.4|^4.0|^5.0
symfony/console Version ^3.4|^4.0|^5.0
symfony/dom-crawler Version ^3.4|^4.0|^5.0