Developed with love by KnpLabs Hire us for your project!
20

money-bundle

by kucharovic

Symfony bundle providing integration for Money library.

GitHub Release
Travis
Total Downloads
SensioLabs Insight
Software License

JKMoneyBundle

This bundle provides integration for Money library in your Symfony
project. Features include:

  • Automatically add Doctrine mappings (use Doctrine embeddable objects)
  • Customized FormType
  • Twig extension

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

$ composer require kucharovic/money-bundle

This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...

            new JK\MoneyBundle\JKMoneyBundle(),
        ];

        // ...
    }

    // ...
}

Step 3: Configuration

By default, bundle load your application locale and define it's currency code as default. You can override it:
```yaml

app/config/config.yml

jk_money:
currency: USD
```

Usage

Entity

// src/AppBundle/Entity/Proudct.php

use Doctrine\ORM\Mapping as ORM;
use Money\Money;

// ...
class Product
{
    // ...

    /**
     * @var Money
     *
     * @ORM\Embedded(class="Money\Money")
     */
    private $price;

    // ...

    public function __construct()
    {
        $this->price = Money::CZK(0);
    }

    public function setPrice(Money $price): void
    {
        $this->price = $price;
    }

    public function getPrice(): Money
    {
        return $this->price;
    }

This entity mapping produces following table structure:

+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| price_amount | varchar(255) | NO | | NULL | |
| price_currency_code | char(3) | NO | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+

So it's easy to query database using aggregate functions like SUM, AVG, etc:

SELECT MAX(`price_amount`), `price_currency_code`
FROM `product`
GROUP BY `price_currency_code`;

Form

Option Type Default
currency string your application locale currency
grouping boolean false
scale integer 2
// src/AppBundle/Entity/Proudct.php

// ...
use JK\MoneyBundle\Form\Type\MoneyType;

class ProductType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('price', MoneyType::class)
        ;
    }

Twig templates

<!-- 1 599,90 Kč -->
Formated with czech locale {{ product.price|money }}<br>
<!-- 1599,9 -->
You can also specify scale, grouping and hide currency symbol {{ product.price|money(1, false, false) }
The MIT License (MIT)

Copyright (c) 2015 - 2017 AzuyaLabs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or

(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.

(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
  • Merge pull request #14 from enumag/patch-4
    By web-flow, 8 months ago
  • Fix hidden dependency on symfony/intl
    By web-flow, 8 months ago
  • Merge pull request #13 from ossinkine/issue-13
    By web-flow, 9 months ago
  • Fix deprecated currency as string in MoneyType
    By ossinkine, 9 months ago
  • Merge pull request #12 from ossinkine/issue-12
    By web-flow, 9 months ago
  • Passing a currency as string is deprecated
    By ossinkine, 9 months ago
  • Drop support PHP 5.5
    By kucharovic, 9 months ago
  • Merge pull request #11 from kucharovic/cs
    By web-flow, 10 months ago
  • Coding style
    By kucharovic, 10 months ago
  • Merge pull request #8 from ossinkine/issue-8
    By web-flow, 10 months ago
  • Prevent using scientific notation when converting integer to string
    By ossinkine, 10 months ago
  • Merge pull request #7 from dadscript/currencies
    By web-flow, 11 months ago
  • Added the ability to use custom currencies
    By Evgeniy Kosarev, 11 months ago
  • Merge pull request #6 from kucharovic/optional-dependencies
    By web-flow, 11 months ago
  • Make Twig and Symfony\Form dependencies as optional
    By kucharovic, 11 months ago
  • Merge pull request #4 from enumag/patch-3
    By web-flow, 11 months ago
  • Run tests on PHP 7.2 as well
    By web-flow, 11 months ago
  • Added Travis CI badge to README
    By kucharovic, 11 months ago
  • Fixed dev requirements to pass Travis tests
    By kucharovic, 11 months ago
  • Tweaked travis.yml
    By kucharovic, 11 months ago
  • Added travis.yml
    By kucharovic, 11 months ago
  • Merge pull request #3 from enumag/patch-2
    By web-flow, 11 months ago
  • Allow Symfony 4
    By web-flow, 11 months ago
  • Fixed whitespace on last line of LICENSE file
    By web-flow, 1 year ago
  • Added SensioLabs Insight badge
    By kucharovic, 1 year ago
  • Added new line at end of LICENSE file
    By kucharovic, 1 year ago
  • Added license file
    By kucharovic, 1 year ago
  • Added badges to README
    By kucharovic, 1 year ago
  • Removed unused property
    By kucharovic, 1 year ago
  • Added newline at end of files
    By kucharovic, 1 year ago