Download the PHP package chippyash/currency without Composer

On this page you can find all versions of the php package chippyash/currency. 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 currency

Chippyash/Currency

Quality Assurance

PHP 7 Build Status Maintainability Test Coverage

Please note:

See the Test Contract

What?

Provides strong type implementation of an ISO-4217 Current Currency. Includes

Uses publicly available currency definitions.

The library is released under the GNU GPL V3 or later license

Why?

Dealing with currencies is a pain in the arse. To get it right you have to wade through loads of stuff about locales, symbols and language names, when in fact all you want is a simple way of defining a currency. This library is aimed at removing a whole bunch of complexity.

You can simply use the supplied Factory method to create a currency object, or if you know you are only dealing with a few chosen currencies, use a utility program to generate 'hard' currencies into you own project namespace.

When

The library was developed to support a 'Simple Accounting' (double entry book-keeping) library.

If you want more, either suggest it, or better still, fork it and provide a pull request. If you feel like helping, the data/symbols.html file needs a/ refactoring into an xml file and b/ having missing symbol definitions added. Take a look at docs/missing-symbols.md as a starting point. Most of the missing information is available on Wikipedia, but it is a manual task to transcribe.

Check out ZF4 Packages for more packages

How

Coding Basics

Create a currency (in your current default locale,) via the Type Factory:

    use Chippyash\Currency\Factory;

    $gbp = Factory::create('GBP');
    //or with an initial value
    $gbp = Factory::create('GBP', 12.26);

Create a currency for a different locale:

    use Chippyash\Currency\Factory;

    Factory::setLocale('fr_FR');
    $euro = Factory::create('EUR');
    //or with an initial value
    $euro = Factory::create('EUR', 12.26);

Create currency directly:

    use Chippyash\Currency\Currency;

    $value = 12.26;
    $code = 'FOO';
    $symbol = 'f';
    $foo = new Currency($value, $code, $symbol);

    //set the precision - and this is where using the Factory starts to 
    // win out unless of course you are creating fantasy currencies
    $precision = 3; //default precision == 2
    $foo = new Currency($value, $code, $symbol, $precision);
    //set long name
    $name = 'Extra Terrestial FooBar'; //default name is the code
    $foo = new Currency($value, $code, $symbol, $precision, $name);
    //supply a display format wrapper
    $displayFormat = 'Yak Yak %s'; //default wrapper is '%s'
    $foo = new Currency($value, $code, $symbol, $precision, $name, $displayFormat);

In all ways, the Currency that you have created acts as an Integer. An additional method is supplied:

display() returns the currency value formatted for the locale that has been set for the currency, e.g. creating:

    Factory::setLocale('en_GB');
    $gbp = Factory::create('GBP', 1200.26);
    echo $gbp->display();

will display:

£1,200.26

whereas

    Factory::setLocale('fr_FR');
    $gbp = Factory::create('GBP', 1200.26);
    echo $gbp->display();

will display:

1 200,26 £

In both cases, $gbp->getValue() will return 120026, i.e. an int.

If you need the value as a float downscaled according to its precision use getAsFloat()

As alluded to above, the Currency class is based on the Integer. This is because integer maths is far more accurate than floating point maths. The class knows how to convert to/from int/float using the precision parameter and can therefore maintain long term accuracy.

You can set its value either via the set() method using an integer value or via the setAsFloat() method using a float value that will be upscaled to the internal integer value using the Currency's precision value. e.g. assuming a precision == 2, then:

    $curr->set(2000);
    //is same as 
    $curr->setAsFloat(20.00);

Please note that the magic __toString() method, will return the value as stringified integer. Use the display() method for locale aware display of the currency.

Generating your own application hard currencies

Let's say that your application is only interested in using AUD, GBP and USD. From a processing point of view, constantly having to query the currencies.xml file, which although optimized, is still large, and is potentially an expensive process.

A utility is provided to enable you generate 'hard' currency classes to disk. You will need to have installed the library via Composer with dev requirements (default for Composer). The utility can be found at bin/generate-currency-class.php.

The parameters (in order of use,) are:

Assuming our current locale is the one we require, we can then run the program 3 times to generate the classes:

bin/generate-currency-class.php aud MyApp\\Currency /home/foo/Projects/MyApp/Currency
bin/generate-currency-class.php gbp MyApp\\Currency /home/foo/Projects/MyApp/Currency
bin/generate-currency-class.php usd MyApp\\Currency /home/foo/Projects/MyApp/Currency

Let's say we also want to support display of these currencies in France. We can generate thus:

bin/generate-currency-class.php aud MyApp\\Currency\\Fr /home/foo/Projects/MyApp/Currency/Fr fr_FR
bin/generate-currency-class.php gbp MyApp\\Currency\\Fr /home/foo/Projects/MyApp/Currency/Fr fr_FR
bin/generate-currency-class.php usd MyApp\\Currency\\Fr /home/foo/Projects/MyApp/Currency/Fr fr_FR

In either case, you'll find three classes have been generated in your target directory:

Changing the library

  1. fork it
  2. write the test
  3. amend it
  4. do a pull request

Found a bug you can't figure out?

  1. fork it
  2. write the test
  3. do a pull request

NB. Make sure you rebase to HEAD before your pull request

Or - raise an issue ticket.

Amending the currencies data sets

As previously stated, we are missing some symbol data in the data set (see docs/missing-symbols.md). If you update any of the files in the data/ directory, you will need to run the bin/create-currency-data.php script to regenerate the src/Chippyash/Currency/currencies.xml file.

Then run the src/Chippyash/Currency/currency-type-xsd.xsl translation against the resulting currencies.xml file to create the src/Chippyash/Currency/currency-type.xsd.

Run these before any pull request to make changes to the data set.

Please note that CLDR do not provide some currency names for a small set of locales. The Currency\Factory will default to using the base language translation if it can find one, or the English name as a backstop.

Where a symbol cannot be found for a Currency, then it will default to using the ISO4217 code as a symbol.

Data set sources

The data sets are sourced from various places:

Where?

The library is hosted at Github. It is available at Packagist.org

Installation

Install Composer

For production

    "chippyash/currency": ">=4,<5"

For development

Clone this repo, and then run Composer in local repo root to pull in dependencies

    git clone [email protected]:chippyash/Currency.git Currency
    cd Currency
    composer install

To run the tests:

    cd Currency
    vendor/bin/phpunit -c test/phpunit.xml test/

License

This software library is released under the BSD 3 Clause license

This software library is Copyright (c) 2015, Ashley Kitson, UK

History

V1.0.0 Initial release

V2.0.0 BC Break: namespace changed from chippyash\Currency to Chippyash\Currency

V2.0.1 Update dev dependencies

V2.0.2 Switch from coveralls to codeclimate

V2.0.3 Add link to packages

V2.0.4 Fix badge links

V2.0.5 Verify PHP 7 compatibility

V2.0.6 Dependency update

V2.1.0 Add currency-type.xsd creation

V3.0.0 PHP 5.3 and 5.4 support withdrawn

V3.0.1 update composer - forced by packagist composer.json format change

V4.0.0 BC Break. Withdraw support for old PHP versions

V4.0.1 fix namespace issue with native get_locale

V4.1.0 ability to get currency precision

V4.2.0 Change of license from GPL V3 to BSD 3 Clause

V5.0.0 Remove dependency on chippyash/strongtype. BC break. Remove support for PHP <7.2


All versions of currency with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2
ext-intl Version *
ext-xml Version *
ext-simplexml Version *
ext-dom Version *
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 chippyash/currency contains the following files

Loading the files please wait ....