Convert Figma logo to code with AI

moneyphp logomoney

PHP implementation of Fowler's Money pattern.

4,597
440
4,597
6

Top Related Projects

2,460

A PHP string manipulation library with multibyte support

Quick Overview

Money is a PHP library that provides a clean and expressive way to work with monetary values and currencies. It offers a robust set of tools for handling financial calculations, currency conversions, and formatting, ensuring precision and consistency in financial applications.

Pros

  • Precise decimal arithmetic, avoiding floating-point errors
  • Extensive currency support with ISO currency codes
  • Immutable value objects for safer operations
  • Comprehensive formatting options for different locales

Cons

  • Requires PHP 7.4 or higher, which may not be available on all hosting environments
  • Learning curve for developers new to value objects and immutable programming concepts
  • Limited built-in support for certain complex financial operations (e.g., compound interest calculations)

Code Examples

Creating a Money object:

use Money\Money;

$fiveDollars = Money::USD(500);

Performing arithmetic operations:

$tenDollars = Money::USD(1000);
$result = $fiveDollars->add($tenDollars);
echo $result->getAmount(); // Outputs: 1500

Currency conversion:

use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Converter;
use Money\Exchange\FixedExchange;

$exchange = new FixedExchange([
    'EUR' => [
        'USD' => 1.1,
    ],
]);

$converter = new Converter(new ISOCurrencies(), $exchange);
$eur = Money::EUR(100);
$usd = $converter->convert($eur, new Currency('USD'));

echo $usd->getAmount(); // Outputs: 110

Getting Started

To use Money in your project, first install it via Composer:

composer require moneyphp/money

Then, you can start using it in your PHP code:

use Money\Money;
use Money\Currency;

$amount = Money::EUR(500);
$currency = new Currency('USD');

$newAmount = Money::USD(1000);

if ($amount->isSameCurrency($newAmount)) {
    $sum = $amount->add($newAmount);
}

echo $sum->getAmount(); // Outputs the sum in cents

This example demonstrates creating Money objects, checking currency compatibility, and performing addition.

Competitor Comparisons

2,460

A PHP string manipulation library with multibyte support

Pros of Stringy

  • Focuses on string manipulation, offering a wide range of string-related functions
  • Provides a fluent interface for chaining multiple string operations
  • Supports multibyte string handling and UTF-8 encoding

Cons of Stringy

  • Limited to string operations, lacking features for other data types
  • May have performance overhead for simple string operations compared to native PHP functions
  • Less actively maintained compared to Money (last commit over 3 years ago)

Code Comparison

Stringy:

use Stringy\Stringy as S;
$string = S::create('fòôbàř');
echo $string->toAscii(); // foobAR

Money:

use Money\Money;
$fiveDollars = Money::USD(500);
$tenDollars = Money::USD(1000);
echo $fiveDollars->add($tenDollars); // $15.00

Summary

Stringy is a specialized library for string manipulation, offering a wide range of functions with a fluent interface. It excels in handling multibyte strings and UTF-8 encoding. However, its scope is limited to string operations, and it may have some performance overhead.

Money, on the other hand, is focused on handling monetary values and currency operations. It provides a robust solution for financial calculations and currency conversions, which is outside the scope of Stringy.

The choice between these libraries depends on the specific needs of your project. If you primarily work with string manipulations, Stringy could be beneficial. For financial applications, Money would be more appropriate.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Money

Latest Version GitHub Workflow Status Total Downloads

Email

Money PHP

PHP library to make working with money safer, easier, and fun!

"If I had a dime for every time I've seen someone use FLOAT to store currency, I'd have $999.997634" -- Bill Karwin

In short: You shouldn't represent monetary values by a float. Wherever you need to represent money, use this Money value object. Since version 3.0 this library uses strings internally in order to support unlimited integers.

<?php

use Money\Money;

$fiveEur = Money::EUR(500);
$tenEur = $fiveEur->add($fiveEur);

list($part1, $part2, $part3) = $tenEur->allocate([1, 1, 1]);
assert($part1->equals(Money::EUR(334)));
assert($part2->equals(Money::EUR(333)));
assert($part3->equals(Money::EUR(333)));

The documentation is available at http://moneyphp.org

Requirements

This library requires the BCMath PHP extension. There might be additional dependencies for specific feature, e.g. the Swap exchange implementation, check the documentation for more information.

Version 4 requires PHP 8.0. For older version of PHP, use version 3 of this library. From version 4.5 this package will only support PHP versions that actually receive updates by PHP itself. If you want to use the package with older PHP versions, you can of course use older versions of this package.

Install

Via Composer

$ composer require moneyphp/money

Features

  • JSON Serialization
  • Big integer support utilizing different, transparent calculation logic upon availability (bcmath, gmp, plain php)
  • Money formatting (including intl formatter)
  • Currency repositories (ISO currencies included)
  • Money exchange (including Swap implementation)

Documentation

Please see the official documentation.

Testing

We try to follow TDD by using phpunit to test this library.

$ composer test

Running the tests in Docker

Money requires a set of dependencies, so you might want to run it in Docker.

First, build the image locally:

$ docker build -t moneyphp .

Then run the tests:

$ docker run --rm -it -v $PWD:/app -w /app moneyphp vendor/bin/phpunit --exclude-group segmentation

Contributing

We would love to see you helping us to make this library better and better. Please keep in mind we do not use suffixes and prefixes in class names, so not CurrenciesInterface, but Currencies. Other than that, Style CI will help you using the same code style as we are using. Please provide tests when creating a PR and clear descriptions of bugs when filing issues.

Security

If you discover any security related issues, please contact us at team@moneyphp.org.

License

The MIT License (MIT). Please see License File for more information.

Acknowledgements

This library is heavily inspired by Martin Fowler's Money pattern. A special remark goes to Mathias Verraes, without his contributions, in code and via his blog, this library would not be where it stands now.