Skip to content

Instantly share code, notes, and snippets.

@SumonMSelim
Created August 6, 2020 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SumonMSelim/9c608145099431cf2ac72888563f91f0 to your computer and use it in GitHub Desktop.
Save SumonMSelim/9c608145099431cf2ac72888563f91f0 to your computer and use it in GitHub Desktop.
Money with BrickMoney
<?php
namespace App\Entity\Embeddable;
use App\Model\Intl\MoneyInterface;
use Brick\Math\BigNumber;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\RoundingMode;
use Brick\Money\Context\CustomContext;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Brick\Money\Money as BrickMoney;
/**
* @ORM\Embeddable()
*/
final class Money implements MoneyInterface
{
const DEFAULT_CURRENCY = 'EUR';
/**
* @var string|float
* @Assert\NotBlank()
*
* @ORM\Column(type="decimal", scale=4, nullable=false, options={"default":0})
*/
private $amount = 0;
/**
* @var string
* @Assert\NotBlank()
* @Assert\Currency()
*
* @ORM\Column(type="string", nullable=false, options={"default":"EUR"})
*/
private $currency = self::DEFAULT_CURRENCY;
/**
* @var BrickMoney
*/
private $money;
/**
* @param string|float $value
* @param string|null $currency
*/
public function __construct($value = null, ?string $currency = null)
{
if (\is_int($value)) {
$value = (string)$value;
}
if (is_float($value)) {
$value = self::floatToString($value);
} else {
$value = (string)$value;
}
if (\preg_match(self::PARSE_REGEXP, $value, $matches) !== 1) {
throw new NumberFormatException(\sprintf('The given value "%s" does not represent a valid number.', $value));
}
$this->amount = $value ?? self::DEFAULT_VALUE;
$this->currency = (\mb_strlen($currency) > 0) ? strtoupper($currency) : self::DEFAULT_CURRENCY;
}
/**
* @return string
*/
public function __toString(): string
{
return sprintf("%s %s", $this->getCurrency(), $this->getAmount());
}
/**
* @param float $float
* @return string
*/
private static function floatToString(float $float): string
{
$currentLocale = \setlocale(LC_NUMERIC, '0');
\setlocale(LC_NUMERIC, 'C');
$result = (string)$float;
\setlocale(LC_NUMERIC, $currentLocale);
return $result;
}
/**
* @param string|float|integer $amount
* @param string|null $currency
* @return Money
*/
public static function create($amount, ?string $currency = null)
{
return new self($amount, $currency);
}
/**
* @param BrickMoney $money
* @return Money
*/
public static function createFromBrick(BrickMoney $money)
{
return new self($money->getAmount(), $money->getCurrency()->getCurrencyCode());
}
/**
* @return BrickMoney
*/
public function getMoney(): BrickMoney
{
if (null === $this->money) {
$this->money = BrickMoney::of($this->getAmount(), $this->getCurrency(), new CustomContext(4), RoundingMode::HALF_UP);
}
return $this->money;
}
/**
* @return string
*/
public function getAmount(): string
{
return $this->amount;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment