Download the PHP package onmoon/money without Composer

On this page you can find all versions of the php package onmoon/money. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package money

OnMoon Money

Latest Version Build License

OnMoon Money is an opinionated wrapper around MoneyPHP Money: https://github.com/moneyphp/money

Installation

The preferred way to install this extension is through composer.

Features

On top of the wonderful API of the original, more strictness and some additional features are added.

Money classes can be extended and used as Doctrine Embeddables

The MoneyPHP objects are final, so you can't create your own domain value objects adding more semantics to the code:

With OnMoon Money you can do this:

Also MoneyPHP Money class stores Currency internally as an object, that is a problem for mapping value objects in Doctrine using embeddables, as the Money object is itself an embeddable and you get nested embeddables:

OnMoon Money class stores currency internally as a string, and can be mapped as one embeddable using the provided Doctrine Types:

Money is created only from strings in strict formats depending on the currency

MoneyPHP allows creating Money objects from a wide range of inputs and requires the input amount to be in subunits of the currency. There is no check how many subunits the currency actually has. This requires you to perform validation and checks in your code and can be error-prone.

OnMoon Money instead accepts ammounts only as strings containing the monetary amount in a human-readable format and strictly enforces the format depending on the currency used.

The same API, but strictly typed

MoneyPHP Money:

OnMoon Money:

etc.

Custom validation for your code extending the library classes with meaningful messages

Usage

For beginning, you should make yourself familiar with the MoneyPHP Money documentation

Currency classes

OnMoon Money provies a class for representing currency values: OnMoon\Money\Currency.

To create a currency object you will need the currency code:

Money classes

The API of a OnMoon Money class is the same as the MoneyPHP Money class:

You can create your own Money classes with your own semantics:

You can create an instance of the specific Money class by using the named constructor:

Subunits

The library provies three base classes that you can use directly or extend from:

OnMoon\Money\Money - can work with currencies with up to 2 subunits

OnMoon\Money\GaapMoney - can work with currencies with up to 4 subunits and conforms with the GAAP standard

OnMoon\Money\BTC - can work with 8 subunits and is restricted to the Bitcoin (XBT) currency

Depending on the base class you use or extend from, some currencies may be unavailable due to requiring more subunits than the base class can work with.

You should choose the base class depending on the currencies that your application will use with the money class.

If you need your own custom subunit amount you can extend any Money class and implement the classSubunits method.

Remember, that you cannot use Money classes of different subunits in the Money class API:

Validation

On top of the validation provided by the base classes, you can enforce additional constraints in your extended classes.

By implementing one of the following methods:

If you need more complex validation logic, implement the following method:

You can also specify the list of currencies that are allowed for a Money class and all classes that extend from it:

The default classes provided by the library support the following currencies:

OnMoon\Money\Money - All ISO currencies with 0-2 subunits

OnMoon\Money\GaapMoney - All ISO currencies with 0-4 subunits

OnMoon\Money\BTC - Only XBT with 8 subunits

All operations on the Money class that change the amount will return the base class instead of the extended, as the resulting amount can violate the invariants of the extended class:

Error handling

Exceptions thrown by OnMoon money classes are extended from two base exceptions:

Examples of OnMoon\Money\Exception\MoneyRuntimeError error messages:

You can make theese messages even more helpful, by implementing the humanReadableName method in your Money Classes:

The error messages then will look like this:

If you want to catch all exceptions thrown by OnMoon Money, including the exceptions of the underlying MoneyPHP Money code - use the Money\Exception interface.

Using the library with Symfony and Doctrine

The library provides four Doctrine types to persist the Money and Currency objects to the database:

The rule of thumb for Type classes mapping the Money object is that the Type class decimal precision should be equal to the Money class subunits. If they will be different, you will get other amounts from the database than previously saved.

Example code

Entity:

Value object:

/config/packages/doctrine.xml:

Entity mapping:

Value object mapping:


All versions of money with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0|^8.1
ext-bcmath Version *
doctrine/dbal Version ^2|^3
moneyphp/money Version ^4.0.3
thecodingmachine/safe Version ^1.3|^2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package onmoon/money contains the following files

Loading the files please wait ....