Giter Site home page Giter Site logo

postscripton / laravel-money Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 4.0 682 KB

💵 Laravel Money is an open source package that provides you a convinient way to work with numbers from database with high precision and use them as monetary objects

Home Page: https://packagist.org/packages/postscripton/laravel-money

License: MIT License

PHP 99.78% Dockerfile 0.22%
currency php convert-numbers database money money-converter laravel rate-exchange formatting exchage-rates

laravel-money's People

Contributors

folez avatar postscripton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-money's Issues

Money field in DB may be nullable

$table->bigInteger('comission')->nullable();

Check that nullable field does not cause an error when casting into monetary object within MoneyCast.

Rename Traits to Partials

It is a bit misleading to use inner traits for personal usage, so actual users won't be confused by namespace PostScripton\Money\Traits

Rework `equals()` method

Make equals() method compare against amount of another monetary object.
The strict flags requires currencies of both monetary objects to be the same.

Pest

I would try to integrate Pest for testing

Blueprint macro

Extend Blueprint methods for monetary fields in migrations.

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        // ...
        $table->money('price');
    });
}

Operating only with objects, not numbers

By the time this feature will have been added, you won't be able to use numbers and origins in operations, only objects.


What I mean:

NO LONGER

money(1000)->add(500);

SINCE v4.0

$income = money(50.0);
money(100.0)->add($income);

This means you do not need to rack your brains to create an object with the desired "origin" (now 100.0 instead of 1000).

Get rid of Currency Lists in the code base

I've decided to get rid of Currency Lists because of their overwhelming compelexity in the realisation. It would be simplier to set a Currency List only in the config file but not in the code.

It doesn't meant to get rid of it entirely but of it from the methods such as:

  • Currency::currentList()
  • Currency::setCurrencyList()
  • Currency::isIncorrectList()
    and of the constants:
  • LIST_ALL
  • LIST_POPULAR
  • LIST_CUSTOM
  • LIST_CONFIG
  • LISTS

Remove reference on Money from Settings

Since Money and Settings refer to each other, so they will never be deleted. (Garbage Collection)
It is considered, Settings don't even need to be bound to Money.

If so, we can bind one instance of Settings to many Money objects, and since the last Money is deleted, the Settings are deleted too.

We can also remove cloning Settings during Money initialization.

Formatting

Extract logic for making a string into a Formatter.
With that MoneySettings may not be needed at all.

Custom short difinitions for currencies

In Russian, we are used to write short definition of ruble like 100 руб., not symbol .
So I'd like to add this functionallity on config level and on built-in currency lists for customizing currencies from different countries.

A use-case may look like:

// config/money.php

return [
    // ...

    'primarily_use_custom_definitions' => true,
    'custom_definitions` => [
        'RUB' => 'руб.'
    ],
];
money(1000)->toString(); // "100 ₽"

money(1000)->withCustomDefinition()->toString(); // "100 руб."

money(1000)->settings()->useCustomDefinition(true)->toString(); // "100 руб."

If there's no custom definition in the config file for the currency, then it just ignores and intended symbol is used ( / RUB).

You can help me out with the method name in the comments below 😄

Get all the currencies

I'd like to get all the currencies that are available as an array:

use PostScripton\Money\Currency;

$currencies = Currency::getCurrencies();
[
  "USD",
  "EUR",
  "RUB",
  // ...
]

Use Carbon\Carbon

Change all the usages of Illuminate\Support\Carbon to Carbon\Carbon

Rework exceptions

There are a lot of exceptions.

  • Should delete a few.
  • Rework them in order to get rid of __METHOD__ everywhere.

Raising precision

NON-integer columns

It's been decided to abandon NON-integer columns in databases.
Now it is always an integer!

The persistent precision is now 4 decimals for databases.

Now the decimals setting is only responsible for displaying money.
Meanwhile, databases will always have 4 decimals no matter what.


DATABASE

id name email balance
1 John [email protected] 12582000

DECIMALS IN CONFIG
1

HOW IT ACTUALLY LOOKS LIKE

$user->balance->toString(); // "$ 1 258.2"

Extract Currency from MoneySettings

Extract Currency from MoneySettings and put it to Money object because it is supposed to be related to the Money object.

- Money
    - amount
    - Currency
    - MoneySettings

Use BCMath calculator

Use BCMath for enormous numbers. This calculator works with strings, and the amount in monetary objects is just a string.

Rework Parser to be able to pass a currency

The main point of this is to help parser to recongnize a currency.

It will take currency from the currency_list property of the config file.
The default currency will be the code from the config property default_currency.

public static function parse(string $money, ?string $currencyCode = null): Money
Money::parse('100.25 ₽', 'RUB')
Money::parse('$100.25', 'USD')
Money::parse('$100.25')

Money::parse('100.25 ₽', 'USD') // throws exception

Add `ClarifyingSymbol` to `CurrencyDisplay`

This means that currencies with, for example, $ symbol will identify their true nature.

For example, both Canadian and American dollars have the same symbol $, and in order to understand which is which, ClarifyingSymbol comes into place.

$cad = currency('CAD')->setDisplay(CurrencyDisplay::ClarifyingSymbol);
$cadMoney = money('2500', $cad);

$usd = currency('USD')->setDisplay(CurrencyDisplay::ClarifyingSymbol);
$usdMoney = money('2500', $usd);

$cadMoney->toString(); // "CA$ 0.25"
$usdMoney->toString(); // "US$ 0.25"

⚠️ This will work only for currencies with the same symbols.

Using currency_list setting as custom doesn't work

Description:

The config value "money.currency_list" must be "all", "popular", "custom" or an array of codes. The value "custom" was given.

Additional Context:

I noticed that the isIncorrectList method of the PostScripton\Money\Currency class is missing the LIST_CUSTOM constant. Will it be something related to this?

Versions:

Laravel Version: 8.x
Laravel Money Version: 3.2
PHP Version: 7.4.28

Steps To Reproduce:

  1. Change currency_list to custom in config/money.php

New helper method for validating money string

That's been mentioned in this issue that it would be useful if some sort of validation function for money-string was added, instead of wrapping money_parse() with try-catch blocks.

Should add something like money_validate() or is_valid_money_string().

Extract Quick Start

Extract Quick Start into a separate MD file to remove it from the main README.


Table of Contents

  1. ⏰ Quick Start
  2. ✅ Usage
  3. ...

Rename Services to RateExchangers

Rename folder, classes and config keys.

In the config, get rid of "secure": bool and "base_restriction": bool keys, and use "plan": string or "(enterprise/pro)": bool instead.

Make rule and extend Laravel's validator

Make a rule for validating requests

Register it in the ServiceProvider:

public function boot()
{
    Validator::extend('money', 'PostScripton\Money\Rules\Money@passes', app(\PostScripton\Money\Rules\Money::class)->message());
}

Please add @throws phpdoc comments to helper functions

It's not always clear that a helper function can throw an exception, it has to have a phpdoc @throws comment.
image

For example lessThan function has an indicator that it throws an exception, I can see from that that I have to deal with this Exception by myself.
image

Also maybe you would like to add is_valid_money_string() function-helper or money_validate() to validate the string before parsing? I want to have a way of avoiding try/catch structures.

Ability to override default currency

Description

Some custom currency uses the taken codes of the already existing currency: "United States dollar" (USD|840)

When I try to override the default currency in the custom list, I got an error.

Steps to reproduce

  1. Change currency_list to custom
  2. Override existing currency in custom_currencies with either iso_code or num_code:
    • For example, I want to override USD

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.