Skip to content

Commit

Permalink
Merge pull request #12 from whitecube/feature/toMinor
Browse files Browse the repository at this point in the history
Added toMinor method for easy access to the underlying integer minor value
  • Loading branch information
toonvandenbos authored Jan 17, 2022
2 parents 0c72dd6 + 52219a5 commit c06c070
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ public function inclusive($perUnit = false)
return $result['amount'];
}

/**
* Shorthand to easily get the underlying minor amount (as an integer)
*
* @param null|string $version
* @return int
*/
public function toMinor($version = null)
{
if ($version === 'inclusive') {
return $this->inclusive()->getMinorAmount()->toInt();
}

if ($version === 'exclusive') {
return $this->exclusive()->getMinorAmount()->toInt();
}

return $this->base()->getMinorAmount()->toInt();
}

/**
* Split given amount into ~equal parts and return the smallest
*
Expand Down
27 changes: 25 additions & 2 deletions tests/Unit/HasValueAccessorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tests\Unit;

use Brick\Money\Context;
use Brick\Money\Money;
use Brick\Money\Currency;
use Whitecube\Price\Price;

Expand All @@ -28,4 +27,28 @@
$context = $price->context();

expect($context)->toBeInstanceOf(Context::class);
});
});

it('can access the base minor value', function() {
$price = Price::EUR(100);

$minor = $price->toMinor();

expect($minor)->toBe(100);
});

it('can access the vat-inclusive minor value', function() {
$price = Price::EUR(100)->setVat(21);

$minor = $price->toMinor('inclusive');

expect($minor)->toBe(121);
});

it('can access the vat-exclusive minor value', function() {
$price = Price::EUR(100)->setVat(21);

$minor = $price->toMinor('exclusive');

expect($minor)->toBe(100);
});

0 comments on commit c06c070

Please sign in to comment.