Giter Site home page Giter Site logo

money's Introduction

Money (DEPRECATED)

Shameless port of RubyMoney

Build Status

Money is a PHP class that allows you to easily work with money within your application including it's value and currency. Each currency has it's associated information encapsulated with a seperate Currency class.

Values are represented as integers in cents to avoid floating point rounding errors.

Return values are immutable value objects.

Installation

Add philipbrown/money as a requirement to composer.json:

{
  "require": {
    "philipbrown/money": "1.*"
  }
}

Update your packages with composer update.

Usage

To create a new Money object you either instantiate like you normally would, or use the init static convenience method.

// Create a new Money object representing $5 USD
$m = Money::init(500, 'USD');
$m = new Money(500, 'USD');

Values are stored as integers to avoid the problem of floating point errors. To access the value of the Money object you can simply request the cents property. To get the currency of the object you can request the currency property. This will return an instance of Money\Curreny that has a __toString method.

$m->cents; // 500
$m->currency; // United States Dollar

Equality is important to working with many different types of currency. You shouldn't be able to blindly add two different currencies without some kind of exchange process.

$m = Money::init(500, 'USD');

$m->isSameCurrency(Money::init(500, 'GBP')); // false

A Value Object is an object that represents an entity whose equality isn't based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.

$one = Money::init(500, 'USD');
$two = Money::init(500, 'USD');
$three = Money::init(501, 'USD');

$one->equals($two); // true
$one->equals($three); // false

Inevitably you are going to need to add, subtract, multiply and divide values of money in your application.

$one = Money::init(500, 'USD');
$two = Money::init(500, 'USD');
$three = $one->add($two);
$three->cents // 1000

Again, you shouldn't be able to add to values of different currencies without some kind of exchange process.

$one = Money::init(500, 'USD');
$two = Money::init(500, 'GBP');

$three = $one->add($two); // Money\Exception\InvalidCurrencyException

License

The MIT License (MIT)

Copyright (c) 2014 Philip Brown

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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.