Skip to content

Commit 5c88b69

Browse files
authored
v2.1.0 (#9)
v2.1.0
2 parents b419615 + 10a78c8 commit 5c88b69

File tree

8 files changed

+472
-8
lines changed

8 files changed

+472
-8
lines changed

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,71 @@ $money->getPureNumber(); // 132.76686139139672
433433

434434
---
435435

436+
##### `add()`
437+
438+
adds a number to the money
439+
440+
```php
441+
use PostScripton\Money\Money;
442+
443+
$money = new Money(1000); // "$ 100"
444+
$money->add(500); // "$ 150"
445+
```
446+
447+
```php
448+
use PostScripton\Money\Money;
449+
use PostScripton\Money\MoneySettings;
450+
451+
$money = new Money(1000); // "$ 100"
452+
$money->add(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 150"
453+
```
454+
455+
---
456+
457+
##### `subtract()`
458+
459+
subtracts a number from the money
460+
461+
```php
462+
use PostScripton\Money\Money;
463+
464+
$money = new Money(1500); // "$ 150"
465+
$money->subtract(500); // "$ 100"
466+
```
467+
468+
```php
469+
use PostScripton\Money\Money;
470+
use PostScripton\Money\MoneySettings;
471+
472+
$money = new Money(1500); // "$ 150"
473+
$money->subtract(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 100"
474+
```
475+
476+
---
477+
478+
##### `rebase()`
479+
480+
a number to which the money will be rebased
481+
482+
```php
483+
use PostScripton\Money\Money;
484+
485+
$money = new Money(1500); // "$ 150"
486+
$money->rebase(100); // "$ 10"
487+
```
488+
489+
```php
490+
use PostScripton\Money\Money;
491+
use PostScripton\Money\MoneySettings;
492+
493+
$money = new Money(1500); // "$ 150"
494+
$money->rebase(10.0, MoneySettings::ORIGIN_FLOAT); // "$ 10"
495+
```
496+
497+
---
498+
436499
##### `convertOfflineInto()`
500+
437501
converts Money object into the chosen currency
438502

439503
```php
@@ -477,4 +541,4 @@ use PostScripton\Money\Money;
477541

478542
$money = new Money(1234);
479543
$money->toString(); // "$ 123.4"
480-
```
544+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "postscripton/laravel-money",
33
"description": "A convenient way to convert numbers from DB or inputs into money strings for humans",
4-
"version": "2.0.0",
4+
"version": "2.1.0",
55
"type": "library",
66
"license": "MIT",
77
"authors": [
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PostScripton\Money\Exceptions;
4+
5+
class NotNumericException extends ValueErrorException
6+
{
7+
public function __construct(
8+
string $method,
9+
int $arg_num,
10+
string $arg_name = null,
11+
$code = 0,
12+
BaseException $previous = null
13+
) {
14+
parent::__construct(
15+
$method,
16+
$arg_num,
17+
$arg_name,
18+
'must be numeric',
19+
null,
20+
$code,
21+
$previous
22+
);
23+
}
24+
}

src/Exceptions/ValueErrorException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public function __construct(
1414
BaseException $previous = null
1515
) {
1616
$error = "ValueError: {$method}(): Argument #{$arg_num} ";
17-
$error .= is_null($arg_name) ?: "({$arg_name}) ";
17+
$error .= is_null($arg_name) ? '' : "({$arg_name}) ";
1818
$error .= "{$err_msg}.";
19-
$error .= is_null($message) ?: " {$message}.";
19+
$error .= is_null($message) ? '' : " {$message}.";
2020

2121
parent::__construct($error, $code, $previous);
2222
}

src/Money.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PostScripton\Money;
44

5+
use PostScripton\Money\Exceptions\NotNumericException;
6+
use PostScripton\Money\Exceptions\UndefinedOriginException;
57
use PostScripton\Money\Traits\MoneyFormatter;
68
use PostScripton\Money\Traits\MoneyStatic;
79

@@ -45,8 +47,8 @@ public function getPureNumber(): float
4547
public function getNumber(): string
4648
{
4749
$amount = $this->settings->getOrigin() === MoneySettings::ORIGIN_INT
48-
? (float)($this->number / $this->getDivisor())
49-
: $this->number;
50+
? (float)($this->getPureNumber() / $this->getDivisor())
51+
: $this->getPureNumber();
5052

5153
$money = number_format(
5254
$amount,
@@ -67,6 +69,67 @@ public function getNumber(): string
6769
return $money;
6870
}
6971

72+
public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money
73+
{
74+
// Error handlers
75+
if (!is_numeric($number)) {
76+
throw new NotNumericException(__METHOD__, 1, '$number');
77+
}
78+
if (!in_array($origin, MoneySettings::ORIGINS)) {
79+
throw new UndefinedOriginException(__METHOD__, 2, '$origin');
80+
}
81+
82+
$this->number += $this->numberIntoCorrectOrigin($number, $origin);
83+
return $this;
84+
}
85+
86+
public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money
87+
{
88+
// Error handlers
89+
if (!is_numeric($number)) {
90+
throw new NotNumericException(__METHOD__, 1, '$number');
91+
}
92+
if (!in_array($origin, MoneySettings::ORIGINS)) {
93+
throw new UndefinedOriginException(__METHOD__, 2, '$origin');
94+
}
95+
96+
$number = $this->numberIntoCorrectOrigin($number, $origin);
97+
98+
// If less than 0, then result must be 0
99+
if ($this->getPureNumber() - $number < 0) {
100+
$number = $this->getPureNumber();
101+
}
102+
103+
$this->number -= $number;
104+
return $this;
105+
}
106+
107+
public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money
108+
{
109+
// Error handlers
110+
if (!is_numeric($number)) {
111+
throw new NotNumericException(__METHOD__, 1, '$number');
112+
}
113+
if (!in_array($origin, MoneySettings::ORIGINS)) {
114+
throw new UndefinedOriginException(__METHOD__, 2, '$origin');
115+
}
116+
117+
$this->number = $this->numberIntoCorrectOrigin($number, $origin);
118+
return $this;
119+
}
120+
121+
private function numberIntoCorrectOrigin($number, int $origin = MoneySettings::ORIGIN_INT)
122+
{
123+
// If origins are not the same
124+
if ($this->settings->getOrigin() !== $origin) {
125+
return $this->settings->getOrigin() === MoneySettings::ORIGIN_INT
126+
? floor($number * $this->getDivisor()) // $origin is float
127+
: $number / $this->getDivisor(); // $origin is int
128+
}
129+
130+
return $number;
131+
}
132+
70133
public function convertOfflineInto(Currency $currency, float $coeff): Money
71134
{
72135
$new_amount = $this->getPureNumber() * $coeff;

src/MoneyInterface.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,41 @@ public function getNumber(): string;
118118
*/
119119
public function getPureNumber(): float;
120120

121+
/**
122+
* Adds a number to the money. It's like <p>
123+
* `$100 + $50 = $150` </p>
124+
* @param $number <p>
125+
* A number that will be added </p>
126+
* @param int $origin <p>
127+
* Origin of the number whether it is integer of float. </p> <p>
128+
* Use `Money::ORIGIN_*` to ensure it's correct </p>
129+
* @return Money
130+
*/
131+
public function add($number, int $origin = MoneySettings::ORIGIN_INT): Money;
132+
133+
/**
134+
* Subtracts a number from the money. It's like <p>
135+
* `$150 - $50 = $100` </p>
136+
* @param $number <p>
137+
* A number that will be subtracted </p>
138+
* @param int $origin <p>
139+
* Origin of the number whether it is integer of float. </p> <p>
140+
* Use `Money::ORIGIN_*` to ensure it's correct </p>
141+
* @return Money
142+
*/
143+
public function subtract($number, int $origin = MoneySettings::ORIGIN_INT): Money;
144+
145+
/**
146+
* Rebases the money on a number
147+
* @param $number <p>
148+
* A number to which the money will be rebased </p>
149+
* @param int $origin <p>
150+
* Origin of the number whether it is integer of float. </p> <p>
151+
* Use `Money::ORIGIN_*` to ensure it's correct </p>
152+
* @return Money
153+
*/
154+
public function rebase($number, int $origin = MoneySettings::ORIGIN_INT): Money;
155+
121156
/**
122157
* Converts money into another currency using coefficient between currencies
123158
* <p>USD -> RUB = 75.79 / 1</p>

src/MoneySettings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MoneySettings implements MoneySettingsInterface
1010
{
1111
public const ORIGIN_INT = 0;
1212
public const ORIGIN_FLOAT = 1;
13-
private const ORIGIN = [
13+
public const ORIGINS = [
1414
self::ORIGIN_INT,
1515
self::ORIGIN_FLOAT,
1616
];
@@ -89,7 +89,7 @@ public function setCurrency(Currency $currency): MoneySettings
8989

9090
public function setOrigin(int $origin): MoneySettings
9191
{
92-
if (!in_array($origin, self::ORIGIN)) {
92+
if (!in_array($origin, self::ORIGINS)) {
9393
throw new UndefinedOriginException(__METHOD__, 1, '$origin');
9494
}
9595

0 commit comments

Comments
 (0)