Coder Social home page Coder Social logo

money-bundle's Introduction

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 "^1.0"

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 = array(
            // ...

            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:

# 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\Money
     *
     * @ORM\Embedded(class="Money\Money")
     */
    private $price;

    // ...

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

    /**
     * Set price
     *
     * @param \Money\Money $price
     *
     * @return Product
     */
    public function setPrice(\Money\Money $price)
    {
        $this->price = $price;
    }

    /**
     * Get price
     *
     * @return \Money\Money
     */
    public function getPrice()
    {
        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) }

money-bundle's People

Contributors

kucharovic avatar hanmac avatar

Watchers

Jáchym Toušek avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.