Skip to content

Commit

Permalink
v2.1.0 (#9)
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
PostScripton authored Apr 30, 2021
2 parents b419615 + 10a78c8 commit 5c88b69
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 8 deletions.
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,71 @@ $money->getPureNumber(); // 132.76686139139672

---

##### `add()`

adds a number to the money

```php
use PostScripton\Money\Money;

$money = new Money(1000); // "$ 100"
$money->add(500); // "$ 150"
```

```php
use PostScripton\Money\Money;
use PostScripton\Money\MoneySettings;

$money = new Money(1000); // "$ 100"
$money->add(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 150"
```

---

##### `subtract()`

subtracts a number from the money

```php
use PostScripton\Money\Money;

$money = new Money(1500); // "$ 150"
$money->subtract(500); // "$ 100"
```

```php
use PostScripton\Money\Money;
use PostScripton\Money\MoneySettings;

$money = new Money(1500); // "$ 150"
$money->subtract(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 100"
```

---

##### `rebase()`

a number to which the money will be rebased

```php
use PostScripton\Money\Money;

$money = new Money(1500); // "$ 150"
$money->rebase(100); // "$ 10"
```

```php
use PostScripton\Money\Money;
use PostScripton\Money\MoneySettings;

$money = new Money(1500); // "$ 150"
$money->rebase(10.0, MoneySettings::ORIGIN_FLOAT); // "$ 10"
```

---

##### `convertOfflineInto()`

converts Money object into the chosen currency

```php
Expand Down Expand Up @@ -477,4 +541,4 @@ use PostScripton\Money\Money;

$money = new Money(1234);
$money->toString(); // "$ 123.4"
```
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "postscripton/laravel-money",
"description": "A convenient way to convert numbers from DB or inputs into money strings for humans",
"version": "2.0.0",
"version": "2.1.0",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
24 changes: 24 additions & 0 deletions src/Exceptions/NotNumericException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PostScripton\Money\Exceptions;

class NotNumericException extends ValueErrorException
{
public function __construct(
string $method,
int $arg_num,
string $arg_name = null,
$code = 0,
BaseException $previous = null
) {
parent::__construct(
$method,
$arg_num,
$arg_name,
'must be numeric',
null,
$code,
$previous
);
}
}
4 changes: 2 additions & 2 deletions src/Exceptions/ValueErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public function __construct(
BaseException $previous = null
) {
$error = "ValueError: {$method}(): Argument #{$arg_num} ";
$error .= is_null($arg_name) ?: "({$arg_name}) ";
$error .= is_null($arg_name) ? '' : "({$arg_name}) ";
$error .= "{$err_msg}.";
$error .= is_null($message) ?: " {$message}.";
$error .= is_null($message) ? '' : " {$message}.";

parent::__construct($error, $code, $previous);
}
Expand Down
67 changes: 65 additions & 2 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PostScripton\Money;

use PostScripton\Money\Exceptions\NotNumericException;
use PostScripton\Money\Exceptions\UndefinedOriginException;
use PostScripton\Money\Traits\MoneyFormatter;
use PostScripton\Money\Traits\MoneyStatic;

Expand Down Expand Up @@ -45,8 +47,8 @@ public function getPureNumber(): float
public function getNumber(): string
{
$amount = $this->settings->getOrigin() === MoneySettings::ORIGIN_INT
? (float)($this->number / $this->getDivisor())
: $this->number;
? (float)($this->getPureNumber() / $this->getDivisor())
: $this->getPureNumber();

$money = number_format(
$amount,
Expand All @@ -67,6 +69,67 @@ public function getNumber(): string
return $money;
}

public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money
{
// Error handlers
if (!is_numeric($number)) {
throw new NotNumericException(__METHOD__, 1, '$number');
}
if (!in_array($origin, MoneySettings::ORIGINS)) {
throw new UndefinedOriginException(__METHOD__, 2, '$origin');
}

$this->number += $this->numberIntoCorrectOrigin($number, $origin);
return $this;
}

public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money
{
// Error handlers
if (!is_numeric($number)) {
throw new NotNumericException(__METHOD__, 1, '$number');
}
if (!in_array($origin, MoneySettings::ORIGINS)) {
throw new UndefinedOriginException(__METHOD__, 2, '$origin');
}

$number = $this->numberIntoCorrectOrigin($number, $origin);

// If less than 0, then result must be 0
if ($this->getPureNumber() - $number < 0) {
$number = $this->getPureNumber();
}

$this->number -= $number;
return $this;
}

public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money
{
// Error handlers
if (!is_numeric($number)) {
throw new NotNumericException(__METHOD__, 1, '$number');
}
if (!in_array($origin, MoneySettings::ORIGINS)) {
throw new UndefinedOriginException(__METHOD__, 2, '$origin');
}

$this->number = $this->numberIntoCorrectOrigin($number, $origin);
return $this;
}

private function numberIntoCorrectOrigin($number, int $origin = MoneySettings::ORIGIN_INT)
{
// If origins are not the same
if ($this->settings->getOrigin() !== $origin) {
return $this->settings->getOrigin() === MoneySettings::ORIGIN_INT
? floor($number * $this->getDivisor()) // $origin is float
: $number / $this->getDivisor(); // $origin is int
}

return $number;
}

public function convertOfflineInto(Currency $currency, float $coeff): Money
{
$new_amount = $this->getPureNumber() * $coeff;
Expand Down
35 changes: 35 additions & 0 deletions src/MoneyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,41 @@ public function getNumber(): string;
*/
public function getPureNumber(): float;

/**
* Adds a number to the money. It's like <p>
* `$100 + $50 = $150` </p>
* @param $number <p>
* A number that will be added </p>
* @param int $origin <p>
* Origin of the number whether it is integer of float. </p> <p>
* Use `Money::ORIGIN_*` to ensure it's correct </p>
* @return Money
*/
public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money;

/**
* Subtracts a number from the money. It's like <p>
* `$150 - $50 = $100` </p>
* @param $number <p>
* A number that will be subtracted </p>
* @param int $origin <p>
* Origin of the number whether it is integer of float. </p> <p>
* Use `Money::ORIGIN_*` to ensure it's correct </p>
* @return Money
*/
public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money;

/**
* Rebases the money on a number
* @param $number <p>
* A number to which the money will be rebased </p>
* @param int $origin <p>
* Origin of the number whether it is integer of float. </p> <p>
* Use `Money::ORIGIN_*` to ensure it's correct </p>
* @return Money
*/
public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money;

/**
* Converts money into another currency using coefficient between currencies
* <p>USD -> RUB = 75.79 / 1</p>
Expand Down
4 changes: 2 additions & 2 deletions src/MoneySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MoneySettings implements MoneySettingsInterface
{
public const ORIGIN_INT = 0;
public const ORIGIN_FLOAT = 1;
private const ORIGIN = [
public const ORIGINS = [
self::ORIGIN_INT,
self::ORIGIN_FLOAT,
];
Expand Down Expand Up @@ -89,7 +89,7 @@ public function setCurrency(Currency $currency): MoneySettings

public function setOrigin(int $origin): MoneySettings
{
if (!in_array($origin, self::ORIGIN)) {
if (!in_array($origin, self::ORIGINS)) {
throw new UndefinedOriginException(__METHOD__, 1, '$origin');
}

Expand Down
Loading

0 comments on commit 5c88b69

Please sign in to comment.