{"id":15489447,"url":"https://github.com/lexik/lexikcurrencybundle","last_synced_at":"2025-04-07T05:16:14.804Z","repository":{"id":3575845,"uuid":"4638479","full_name":"lexik/LexikCurrencyBundle","owner":"lexik","description":"This Symfony2 bundle provide a service and a Twig extension to convert and display currencies.","archived":false,"fork":false,"pushed_at":"2022-02-13T16:46:15.000Z","size":100,"stargazers_count":58,"open_issues_count":10,"forks_count":40,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-04-25T22:21:56.132Z","etag":null,"topics":["bundle","php","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lexik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-12T14:39:17.000Z","updated_at":"2022-03-31T15:37:27.000Z","dependencies_parsed_at":"2022-08-22T10:11:04.753Z","dependency_job_id":null,"html_url":"https://github.com/lexik/LexikCurrencyBundle","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexik%2FLexikCurrencyBundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexik%2FLexikCurrencyBundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexik%2FLexikCurrencyBundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lexik%2FLexikCurrencyBundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lexik","download_url":"https://codeload.github.com/lexik/LexikCurrencyBundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595335,"owners_count":20963943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bundle","php","symfony","symfony-bundle"],"created_at":"2024-10-02T07:05:48.133Z","updated_at":"2025-04-07T05:16:14.775Z","avatar_url":"https://github.com/lexik.png","language":"PHP","readme":"Overview\n========\n\nThis Symfony2 bundle provide a service and a twig extension to convert and display currencies.\n\n[![Build Status](https://secure.travis-ci.org/lexik/LexikCurrencyBundle.png?branch=master)](http://travis-ci.org/lexik/LexikCurrencyBundle)\n[![Latest Stable Version](https://poser.pugx.org/lexik/currency-bundle/v/stable)](https://packagist.org/packages/lexik/currency-bundle)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/04079218-2ad1-439d-bfab-1c931468147c/mini.png)](https://insight.sensiolabs.com/projects/04079218-2ad1-439d-bfab-1c931468147c)\n\nInstallation\n============\n\nAdd the bunde to your `composer.json` file:\n\n```javascript\nrequire: {\n    // ...\n    \"lexik/currency-bundle\": \"~2.0\"\n    // ...\n}\n```\n\n**As of version `1.2.0`, `currency_format` does not convert the currency anymore, it only formats the given value according to the locale. If you need to convert and format a value, please use `currency_convert_format` filter.**\n\nThen run a composer update:\n\n```shell\ncomposer.phar update\n# OR\ncomposer.phar update lexik/currency-bundle # to only update the bundle\n```\n\nRegister the bundle with your kernel:\n\n```\n// in AppKernel::registerBundles()\n$bundles = array(\n    // ...\n    new Lexik\\Bundle\\CurrencyBundle\\LexikCurrencyBundle(),\n    // ...\n);\n```\n\nConfiguration\n=============\n\nMinimun configuration:\n\n```yaml\n# app/config/config.yml\nlexik_currency:\n    currencies:\n        default: EUR              # [required] the default currency\n        managed: [EUR, USD, ...]  # [required] all currencies used in your app\n```\n\nAdditonal options (default values are shown here):\n\n```yaml\n# app/config/config.yml\nlexik_currency:\n    decimal_part:\n        precision:  2                           # number of digits for the decimal part\n        round_mode: up                          # round mode to use (up|down|even|odd)\n\tcurrency_class: Lexik\\Bundle\\CurrencyBundle\\Entity\\Currency  # Use your custom Currency Entity\n    default_adapter: doctrine_currency_adapter  # service id OR tag alias, this is adapter used by the conversion service\n```\n\nInitialize currencies\n=====================\n\nTo initialize the currencies rate in the database run the following command:\n\n```\n./app/console lexik:currency:import \u003ccurrency adapter identifier\u003e\n```\n\nExample by using the ECB adapter, to get rates from the European Central Bank.\nIn the command line `ecb` is the value returned by the `getIdentifier()` method of the adapter class.\n\n```\n./app/console lexik:currency:import ecb\n```\n\nUsage\n=====\n\n##### Currency conversion service\n\nUse the `convert()` method from the `lexik_currency.converter` service:\n\n```php\n\u003c?php\n// by default the amount will rounded and the amount have to be in the default currency\n$convertedAmount = $container-\u003eget('lexik_currency.converter')-\u003econvert($amount, $targetCurrency);\n\n// here the amount won't be rounded and we specify that $amount currency is 'USD'\n$convertedAmount = $container-\u003eget('lexik_currency.converter')-\u003econvert($amount, $targetCurrency, false, 'USD');\n```\n\n##### Retrieve managed configurations\n\nIn the controller, you can use the following line to retrieve an array of all managed currencies:\n\n```php\n$managedCurrencies = $this-\u003econtainer-\u003egetParameter('lexik_currency.currencies.managed');\n```\n\n##### Twig filters\n\nThe bundle provide 3 filters to convert and format a value:\n* `currency_convert`: convert a value.\n* `currency_format`: format a value according to the current locale.\n* `currency_convert_format`: convert and format a value.\n\nHere an example with the `currency_convert_format` filter.\n\n```\n{% set targetCurrency = 'EUR' %}\n{{ amount | currency_convert_format(targetCurrency) }}\n```\n\nYou can also pass more arguments, to display or not decimal and the currency symbol. And you can specify the amount's currency if needed.\n\n```\n{% set targetCurrency = 'EUR' %}\n{% set amountCurrency = 'USD' %}\n{% set decimal = false %}\n{% set symbol = true %}\n\n{{ amount | currency_convert_format(targetCurrency, decimal, symbol, amountCurrency) }}\n```\n\n##### Load conversions rate from another source (custom CurrencyAdatpter)\n\nIf you need to load conversions rates from another source you will have to create a CurrencyAdatpter and set it as the default adapter.\n\nTo create your custom adapter you will have to extend `Lexik\\Bundle\\CurrencyBundle\\Adapte\\AbstractCurrencyAdapter` which define 2 abstract methods:\n* getIdentifier(): returns the identifier of the adapter.\n* attachAll(): loads the currencies with their rate (this method is call from the import command to get all currencies to save in the database).\n\nHere an example\n\n```php\n\u003c?php\n\nnamespace MyProject\\With\\Some\\Rainbows;\n\nuse Lexik\\Bundle\\CurrencyBundle\\Adapter\\AbstractCurrencyAdapter;\n\nclass RainbowCurrencyAdapter extends AbstractCurrencyAdapter\n{\n\t/**\n     * {@inheritdoc}\n     */\n    public function attachAll()\n    {\n    \t$defaultRate = 1;\n\n        // Add default currency (euro in this example)\n        $euro = new $this-\u003ecurrencyClass;\n        $euro-\u003esetCode('EUR');\n        $euro-\u003esetRate($defaultRate);\n\n        $this[$euro-\u003egetCode()] = $euro;\n\n        // Get other currencies\n        $currencies = // get all currencies with their rate (from a file, an url, etc)\n\n        foreach ($currencies as $code =\u003e $rate) {\n            if (in_array($code, $this-\u003emanagedCurrencies)) { // you can check if the currency is in the managed currencies\n                $currency = new $this-\u003ecurrencyClass;\n                $currency-\u003esetCode($code);\n                $currency-\u003esetRate($rate);\n\n                $this[$currency-\u003egetCode()] = $currency;\n            }\n        }\n\n        // get the default rate from the default currency defined in the configuration\n        if (isset($this[$this-\u003edefaultCurrency])) {\n            $defaultRate = $this[$this-\u003edefaultCurrency]-\u003egetRate();\n        }\n\n        // convert rates according to the default one.\n        $this-\u003econvertAll($defaultRate);\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getIdentifier()\n    {\n        return 'rainbow';\n    }\n}\n```\n\nThen define the adapter as a service, don't forget the `lexik_currency.adapter` tag:\n\n```xml\n\u003cservice id=\"my_project.rainbow_currency_adapter\" class=\"MyProject\\With\\Some\\Rainbows\\RainbowCurrencyAdapter\"\u003e\n    \u003ccall method=\"setDefaultCurrency\"\u003e\n        \u003cargument\u003e%lexik_currency.currencies.default%\u003c/argument\u003e\n    \u003c/call\u003e\n    \u003ccall method=\"setManagedCurrencies\"\u003e\n        \u003cargument\u003e%lexik_currency.currencies.managed%\u003c/argument\u003e\n    \u003c/call\u003e\n    \u003ccall method=\"setCurrencyClass\"\u003e\n        \u003cargument\u003e%lexik_currency.currency_class%\u003c/argument\u003e\n    \u003c/call\u003e\n    \u003ctag name=\"lexik_currency.adapter\" alias=\"rainbow_currency_adapter\" /\u003e\n\u003c/service\u003e\n```\n\nAnd import the currencies by using your adapter:\n\n```\n./app/console lexik:currency:import rainbow\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flexik%2Flexikcurrencybundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flexik%2Flexikcurrencybundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flexik%2Flexikcurrencybundle/lists"}